DISQUS

DISQUS Hello! Tarek Mahmud Apu is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Custom Event in actionscript 3

Started by apueee · 8 months ago

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 anyw ... Continue reading »

5 comments

  • 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?
  • 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?
  • 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.
  • 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
  • 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.

Add New Comment

Returning? Login