Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,613 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 922 people online right now. Registration is fast and FREE... Join Now!




Creating a custom event dispatcher

 
Reply to this topicStart new topic

> Creating a custom event dispatcher, [Flash 8]

BetaWar
Group Icon



post 3 Jul, 2008 - 08:53 PM
Post #1


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.

Here is a look at the whole code we wil be writing in this tutorial:

CODE
_root.listener = new Object();
    _root.listener.obj_array = new Array();
    _root.listener.e_array = new Array();
    _root.listener.caller_array = new Array();
    _root.listener.insert = function(caller:MovieClip, obj:MovieClip, e:Function){
        _root.listener.obj_array.push(obj);
        _root.listener.e_array.push(e);
        _root.listener.caller_array.push(caller);
    }
    _root.listener.remove = function(caller:MovieClip, obj:MovieClip, e:Function){
        find_obj = _root.listener.obj_array;
        find_e = _root.listener.e_array;
        find_caller = _root.listener.caller_array;
        for(i=0; i<find_obj.length; i++){
            if(find_obj[i] == obj && find_e[i] == e && find_caller[i] == caller){
                delete _root.listener.obj_array[i];
                delete _root.listener.e_array[i];
                delete _root.listener.caller_array[i];
                break;
            }
        }
    }
    _root.listener.dispatch = function(caller:MovieClip, e:Function){
        find_obj = _root.listener.obj_array;
        find_e = _root.listener.e_array;
        find_caller = _root.listener.caller_array;
        for(i=0; i<find_obj.length; i++){
            if(find_e[i] == e && find_caller[i] == caller){
                find_obj[i][e]();
            }
        }
    }


Brief overview:
The above code simply creates an object _root.listener and gives it the three functions insert remove 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.

CODE
    _root.listener.obj_array = new Array();
    _root.listener.e_array = new Array();
    _root.listener.caller_array = new Array();

The above lines of code create three variables (obj_array, e_array, and caller_array) which will be populated as you insert and remove items from them.

Inserting items into the arrays is as easy as calling this next function:
CODE
    _root.listener.insert = function(caller:MovieClip, obj:MovieClip, e:Function){
        _root.listener.obj_array.push(obj);
        _root.listener.e_array.push(e);
        _root.listener.caller_array.push(caller);
    }

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.
CODE
    _root.listener.remove = function(caller:MovieClip, obj:MovieClip, e:Function){
        find_obj = _root.listener.obj_array;
        find_e = _root.listener.e_array;
        find_caller = _root.listener.caller_array;
        for(i=0; i<find_obj.length; i++){
            if(find_obj[i] == obj && find_e[i] == e && find_caller[i] == caller){
                delete _root.listener.obj_array[i];
                delete _root.listener.e_array[i];
                delete _root.listener.caller_array[i];
                break;
            }
        }
    }

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:
CODE
    _root.listener.dispatch = function(caller:MovieClip, e:Function){
        find_obj = _root.listener.obj_array;
        find_e = _root.listener.e_array;
        find_caller = _root.listener.caller_array;
        for(i=0; i<find_obj.length; i++){
            if(find_e[i] == e && find_caller[i] == caller){
                find_obj[i][e]();
            }
        }
    }

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.
Attached File  tutorials.zip ( 6k ) Number of downloads: 47
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:43AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month