Okay, now that you know (more or less) how to use the lash built in broadcaster and event listeners we will look into make one of your own.

But first, what are the benifits of making your own code to do premade tasks?
The answer to this is quite simple. When you make something your own you are able to edit how it works, and debug when you think it isn't working correctly. It also improves your knowledge with a given language (in this case ActionScript), which will help you out when learning a language, or just keeping up to date with a language you already know.

Brief overview:
The above code simply creates an object _root.listener and gives it the three functions insertremove and dispatch. These function serve their prupose (which is fairly self-explanatory, but will be covered a little later on in the tutorial) to make everything run well, and hopefully fast enough to make it worth using.

When you call insert (named as such because Flash already has pre made functions that use push and add) it take the three parameters (caller, obj, and e). The caller variable is a pointer to the movieclip that will dispatch the said event (e) and obj is a pointer to the movieclip thatshould be listening for the event call. Each of these are added into their respective arrays for later usage.

The remove function takes the three parameters and loops through all the items in the array looking for when the parameters match the inserted data, deletes the data from the array and breaks out of the loop (there should only be one instance of a given event for a given object from a given caller).

Then there is the dispatch function that goes through (again looping) and "dispatches" calls to the obj's instance of given function when the caller and event are the same as in the array data. This doesn't stop after one because it would be pointless to have multiple objects listening for a event if only one of them would get the request.

Indepth code description and summary_root.listener = new Object(); creates an object called listener.

Which will take the parameters (caller, obj, and e (event)) and insert them (array.push) into their respective arrays. An example of inserting an item into a (fictional) array looks like so _root.array_name.push(new_array_item). That will put the information contained inside of new_array_item at the end of the array, no matter the length.

The next function is used whenever you are wanting to delete (remove) items from the arrays.

This takes the same parameters as the insert function, but then loops through all the items in the arrays and looks for an exact match between them (all three parameters match their counterparts (respectively) in the arrays). When it finds a match it goes through and deletes the item from the array (though in reality it is set to a null value of something like so "", because you can still call to it, it just has nothing to return). The delete is accomplished with this (example is fictional) delete _root.array_name[new_array_item]; where new_array_item is something you are wanting to get rid of in the list of items. After it has completed deleting the one item from the arrays the function breaks out of the loop break; and the function is finished.

Now that the functions are set up to insert and remove items from the array you need a way to dispatch your events to the eager listening movieclips. This is achieved through the following:

Above we create a function that takes 2 parameters (caller, and e (event)), then loops through all the items in the arrays until it finds one that matches. At this point it goes off and calls the object's instance of the event (it is a function, implemented as so: movieclip.ONSOMEEVENT = function(){ trace("yay");} where ONSOMEEVENT is the event being called and movieclip is set up to listen for event calls from caller. This is accomplished through the line stating find_obj[i][e](); which is calling the instance of "e" (which must be a function name of the movieclip that is listening for it) where "i" is a number.

That is how the code works.

Implementation
To use the code above successfully you could spend hours messing with it (assuming you don't know exactly what you are doing), but I will make it easy, and have attached an example file using the above code to make a custom event and dispatcher.tutorials.zip(6K)Number of downloads: 537