9

Can any one give me a simple example on how to dispatch an event in actionscript3 with an object attached to it, like

dispatchEvent( new Event(GOT_RESULT,result));

Here result is an object that I want to pass along with the event.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
user1022521
  • 509
  • 3
  • 9
  • 16
  • 1
    You gave an example yourself. Where exactly are you stuck? – bfavaretto Sep 25 '12 at 19:56
  • Thanks for the reply, I am new to as3 and would like to know how to pass an object with dispatch event? – user1022521 Sep 25 '12 at 19:59
  • 1
    You have to extend the Event class, to create your own custom event object. See an example here: http://cookbooks.adobe.com/post_AS3__Creating_and_dispatching_Custom_Events-17609.html – bfavaretto Sep 25 '12 at 20:00
  • In my case the result is of type classA - so i am not sure if i have to write a custom event or not, if i have to how to write it.. – user1022521 Sep 25 '12 at 20:01

3 Answers3

33

In case you want to pass an object through an event you should create a custom event. The code should be something like this.

public class MyEvent extends Event
{
    public static const GOT_RESULT:String = "gotResult";

    // this is the object you want to pass through your event.
    public var result:Object;

    public function MyEvent(type:String, result:Object, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
        this.result = result;
    }

    // always create a clone() method for events in case you want to redispatch them.
    public override function clone():Event
    {
        return new MyEvent(type, result, bubbles, cancelable);
    }
}

Then you can use the code above like this:

dispatchEvent(new MyEvent(MyEvent.GOT_RESULT, result));

And you listen for this event where necessary.

addEventListener(MyEvent.GOT_RESULT, myEventHandler);
// more code to follow here...
protected function myEventHandler(event:MyEvent):void
{
    var myResult:Object = event.result; // this is how you use the event's property.
}
  • This is how it should be done. Some small additions; I always use a getter field instead of a public field for event data. I would also rename GOT_RESULT to RESULT to keep it clear. – Mark Knol Sep 25 '12 at 22:41
  • 3
    Btw I created an online tool to create custom as3 events http://projects.stroep.nl/EventGenerator/ it could help setup faster. Feel free to use. – Mark Knol Sep 25 '12 at 22:47
  • 1
    I agree about renaming the event constant to RESULT, I was just trying to keep things as close as possible to the OP's example variable names to avoid any confusion that might arise. – Tomislav Dyulgerov Sep 26 '12 at 13:00
  • @ Tomislav Dyulgerov: Hey Thanks man...ur code absolutely worked, thanks for all the help I appreciate it. – user1022521 Sep 26 '12 at 22:24
3

This post is a little old but if it can help someone, you can use DataEvent class like so:

dispatchEvent(new DataEvent(YOUR_EVENT_ID, true, false, data));

Documentation

g0tcha-
  • 323
  • 3
  • 5
0

If designed properly you shouldn't have to pass an object to the event.
Instead you should make a public var on the dispatching class.

public var myObject:Object;

// before you dispatch the event assign the object to your class var
myObject = ....// whatever it is your want to pass
// When you dispatch an event you can do it with already created events or like Tomislav wrote and create a custom class.

// in the call back just use currentTarget
public function myCallBackFunction(event:Event):void{

  // typecast the event target object
  var myClass:myClassThatDispatchedtheEvent = event.currentTarget as myClassThatDispatchedtheEvent 
  trace( myClass.myObject )// the object or var you want from the dispatching class.


The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • Are you saying that passing an object is a bad design decision, or just giving an alternative? I'm asking because the Flash/Flex APIs seem to pass objects quite frequently. – bfavaretto Sep 25 '12 at 21:09
  • 4
    If I understand what you just said; I respectfully disagree. Adding values to event objects is good encapsulation. In many cases, I would prefer that approach as opposed to introspecting the currentTarget (or target) to retrieve data from public vars on an instance. I find this especially beneficial In a display list where an event will bubble up multiple levels in the display hierarchy. – JeffryHouser Sep 25 '12 at 21:09
  • 3
    While I use this methodology at times, it's not a good fit for every scenario (your first sentence would suggest that). eg, if you have multiple classes that dispatch the same event (but don't have a common sub-class), in those cases a custom event is a much cleaner route. Plus there's something to be said about separation of interest. If the class that dispatches the event doesn't have a reason to know about the class that is listening/handling the event, then a custom event is better for that situation as well. Makes unit testing MUCH easier – BadFeelingAboutThis Sep 25 '12 at 21:16
  • Wow this seems to be a touchy subject – The_asMan Sep 26 '12 at 13:43
  • @bfavaretto Yes this was meant more as an alternative. Every pattern has its pros and cons. – The_asMan Sep 26 '12 at 13:55
  • @LondonDrugs_MediaServices I disagree. In my example the dispatching object could "store" the data in its own var and yet still be considered encapsulated. You could always implement an interface if you want to only expose certain members. In any case this is well beyond the scope of the question and I was simply adding another method to get the data exposed. – The_asMan Sep 26 '12 at 19:23