Tuesday, January 11, 2011

Capturing Events in Flash : Part II - Creating Event Handlers

We have already seen few common event classes, let us now try to create a handler for them.
Let us suppose, we have a movieclip, simpleBtn (already present in the respective fla file), we will add a mouse event to it.

this.simplBtn.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);

If you would observe, "addEventListener" takes 5 arguments.

Argument 1: The type of event to capture (in this case its the mouse click event)
Argument 2: The follow up function on dispatching the mouse click event (in this case it is onClick)

The first two arguments are mandatory to be written, whereas the third, fourth, and fifth are optional.

Argument 3: It determines whether the listener works in the capture phase or the target and bubbling phases. If it is set to true, the listener processes the event only during the capture phase and not in the target or bubbling phase. If it is false, the listener processes the event only during the target or bubbling phase.
Argument 4: It is an integer that sets the priority of the listener. Because a single object can use multiple listeners for the same event, it is sometimes useful to specify the order in which the similar listeners are executed. The default value for the priority parameter is 0
Argument 5: It determines whether the reference to the listener is strong or weak. A strong reference (the default as "false") prevents your listener from being garbage-collected. A weak reference does not.

Adding arguments to the function called, when the event is dispatched:

Suppose we want to display a text at simpleTxt, the value of which is passed inside the event listener:

                 this.simplBtn.addEventListener(MouseEvent.CLICK,
                           function(e : MouseEvent) : void
                          {
                                    onClick(e, "button clicked!")
                          },
               false , 0 , false);


               function onClick(e : MouseEvent, str:String) : void
               {
                          this.simpleTxt.text = str;
               }


Hence, the complete code for creating a simple event handler, can be written as follows:

package com{   

           import flash.events.MouseEvent;  

           public class CreatingEventHandler extends Event  
           {      
                      public function CreatingEventHandler()
                      {
                                  init();
                      }


                     private function init():void
                     {
                                  this.simplBtn.addEventListener(MouseEvent.CLICK,
                                  function(e : MouseEvent) : void
                                  {
                                               onClick(e, "button clicked!")
                                  },
                                  false , 0 , false);
                      }


                      private function onClick(e : MouseEvent, str:String) : void
                      {
                                  this.simpleTxt.text = str;
                      }
          }
}

No comments:

Post a Comment