logo

Custom Event in actionscript 3

logo

Adding custom event in as3 can make life easy. Sometime it needs to set root variable from different class or need to call root function for a specific application. Its better to use custom event than using "root" in class. Its easy to trigger and also can be listen from anywhere. Its make a class independent. Creating custom event handlers in ActionScript 3 is pretty much simple. Here i’m showing a simple myCustomEvent class which extends the flash Event class.

   1: //Declare a class for myCustomEvent
   2: package wneeds.events{
   3:     
   4:     import flash.events.Event;
   5:  
   6:     public class myCustomEvent extends Event {
   7:         
   8:         public var myMessage:String;
   9:         
  10:         public function myCustomEvent(msg) {
  11:             
  12:             super("myCustomEvent");
  13:             myMessage = msg;
  14:         }
  15:     }
  16: }

In the above class i also declare a variable named "myMessage" to show how to pass variable in a custom event class. For triggering the custom event, it just need to include the class and use the dispatchEvent method as like below…

   1: //How to asign a event
   2: import wneeds.events.myCustomEvent;
   3:  
   4: dispatchEvent(new myCustomEvent("Hello ..."));

Now time to add Listener to listen the event from anywhere….

   1: //How to add the listener for the myCustomEvent..
   2:  
   3: this.addEventListener("myCustomEvent", showMessage);
   4:  
   5: function showMessage(e:myCustomEvent):void {
   6:     trace(e.myMessage);
   7: }

In various purpose its better to pass object. Because in object its easy to pass multiple properties and easy to write in json format.

   1: dispatchEvent(new myCustomEvent({name:'tma',height: 100, msg : 'Hello...'}));

Then it can be easily possible to get all the properties from the listener…. :)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • Propeller
  • Simpy
  • Socialogs
  • StumbleUpon
  • Technorati
  • Wists

Viewing 5 Comments

    • ^
    • v
    Hi,
    great tutorial, i got it so far working as i can send the one var back to e.myMessage

    But when i try you example line with multi values , i get stuck and only get erros.

    can you make a example how the myCustomEvent class needs to look if i
    dipatch this message.:
    dispatchEvent(new myCustomEvent({Amode:'tma',SID:this.SID,MYID:this.MYID,PRID:this.PRID,TYP:this.TYP}));

    where SID, MYID,PRID,TYP are all byte arrays.
    Best regards Oliver
    • ^
    • v
    Dear Oliver,

    Thanks for ur comment.

    Have u changed at myCustomEvent ? in myCustomEvent class i have set the type of myMessage as string. But you are passing the message as object. So its need to change the myMessage as Object or u can set * for any type.

    package wneeds.events{

    import flash.events.Event;

    public class myCustomEvent extends Event {

    public var myMessage:*;

    public function myCustomEvent(msg) {

    super("myCustomEvent");
    myMessage = msg;
    }
    }
    }

    Try it . i think it should work.
    • ^
    • v
    I'm still confused about custom event. I have search many many sample but it's still make me don't get it. I like the sample that you have above, but I don't know which code will act as document class. Thus, in the part ..."dispatchEvent("new MyCustomEvent("Hello....")); >> it's still the same class with the myCustomEvent class or other new class??
    Oh yeah, this custom Event can do something like accessing other movieclip object that have linkage with a class, can it?
    • ^
    • v
    Dear Purwanto

    Thanks for ur comment.

    The following code need to write where you want to call the event. first u need to import the custom event class. then u need to call the dispatchEvent method to call ur custom event. This is not a part of myCustomEvent Class. Here you just using the myCustomEvent class. :)
    **************
    import wneeds.events.myCustomEvent; //import ur class here
    dispatchEvent(new myCustomEvent("Hello ...")); // call your custom event.
    *******************

    To track the event in any where of ur code u need to put the following code:
    **********
    addEventListener("myCustomEvent", showMessage);
    function showMessage(e:myCustomEvent):void {
    trace(e.myMessage);
    }
    *********************
    I'm not clear about your last question. Do you want to call a event from a movieclip and receive the event from main timeline?
    • ^
    • v
    Not from the timeline, but receive event from other class (that linkaged by a movieclip). Example, from first class after finished some tween, I want to control other movieclip that do something. To do that, I want to access other class from the first class. After access that class, I can control that Movieclip.

Trackbacks

close Reblog this comment
blog comments powered by Disqus
logo
logo
Powered by Wordpress | Designed by Elegant Themes
3 visitors online now
3 guests, 0 members
Max visitors today: 4 at 01:49 am GMT+8
This month: 5 at 09-01-2010 02:00 am GMT+8
This year: 46 at 01-15-2010 12:41 am GMT+8
All time: 51 at 10-22-2009 08:26 am GMT+8