Wednesday, January 19, 2011

Capturing Events in Flash : Part III - Creating Custom Events

The way we use the in-built events in action script, similarly we can create our own events and handle them at our own requirements. The only extra thing that needs to be done is the creation of class which will hold our user-defined or custom events.

However, there are few things to be noted in case of custom events. For the in-built event handling, we have a class called "clone( )". As by the name, its clear that it is something to do with a "copy". Basically, clone( ) function creates a copy of the event instance with all the properties associated to it. This is handled implicitly for the pre-defined events. But for the custom events, we need to override this function to our custom event class. This will help duplicate the custom event instance with all the custom properties associated to it.

Why do we need clone( ) function ?

With clone( ) function, the new event object includes all the properties of the original event. Hence, if the event is re-dispatched, then it takes the properties of the already dispatched same event. If you do not set all the properties that you add in your event subclass, those properties will not have the correct values when listeners handle the re-dispatched event.

We can pass arguments as well to these custom events, which also should be cloned.

Another function that needs to be written explicitly for a custom event class is "toString( )". It returns a string containing all the properties of the current instance. This is also to be overridden in the custom event class.
  
Now, let us assume we want to dispatch an event on completion of some animation, event name say "ANIMATION_COMPLETE". We will add an event listener to the movie clip, and will dispatch the event from the last frame, after which the event is handled accordingly.

Check out the code sample below:

package 
{  
         import flash.events.Event;  

         public class CustomEventDemo extends Event  
         {  
                    public var customProperty:String = "";   

                    public var customArgs:*;

                    public static const ANIMATION_COMPLETE:String = "AnimationComplete";
 
                    public function CustomEventDemo(custom:String, custArg:*=null, type:String, bubbles:Boolean = false, cancelable:Boolean = false)  
                   {  
                                // call the constructor of the extended class  
                                 super(type, bubbles, cancelable);  
 
                                // set the customProperty property  
                                customProperty = custom;   


                                // set the customArgs arguments
                                customArgs = custArg;
                   }   


                   public function get args():*
                   {
                                return customArgs;
                   }
 
                   override public function clone():Event  
                   {  
                                return new CustomEventDemo(customProperty, args, type, bubbles, cancelable);  
                   }   


                   override public function toString():String
                   {
                                return formatToString("CustomEventDemo", "args", "type", "bubbles", "cancelable", "eventPhase");
                   }
 
         }  
 
}

No comments:

Post a Comment