Flex Custom Event dispatching

北城余情 提交于 2019-12-08 03:34:27

You are not listening to the event in the right way as I see it.

 // you have 
 this.addEventListener("UserInfoEvent", userInfoHandler);

 // but you dispatch the event from    
 dispatch.dispatchEvent(userInfoEvent);  

which is wrong

You need to have something like

instance_of_Dispatch.addEventListener();  // because this != dispatch

I think your event is not getting caught because it doesn't bubble

instead of

super(type);

try

super(type, true, false);

If you call the Event constructor with one argument, it defaults to false for the other two arguments, one of which is bubbles.

I think your other problem is that you're creating a new EventDispatcher. Instead, extend EventDispatcher with your service class and then just call dispatch().

So events in Flex are identified by their string names. Your Event class doesn't have a 'name', so it doesn't have any way of identifying it and matching it when it's dispatched. This 'name' is what's passed into the 'type' parameter of the constructor. It's traditionally stored and referenced as a static constant. Since you're comparing the class object UserInfoEvent to a string "UserInfoEvent" it never matches and catches that this is the event that you want to handle.

package events
{
    import flash.events.Event;

    import logic.Ego;

    public class UserInfoEvent extends Event
    {

        public static const MY_EVENT:String = "myEvent";
        private var ego:Ego;

        public function UserInfoEvent(type:String, ego:Ego)
        {
            super(type);
            this.ego = ego;
        }

        override public function clone():Event 
        { 
            return new UserInfoEvent(type, ego);
        } 
    }
}

And then you'd listen / dispatch this event using:

dispatchEvent(new UserInfoEvent(UserInfoEvent.MY_EVENT));

and

addEventListener(UserInfoEvent.MY_EVENT, myEventHandler);

Hopefully that makes sense, i'm fighting off a nasty migraine right now and it's killing my ability to use words:P

I'm guessing your event dispatcher is bubbling up to the systemManager rather than up through the main app since your dispatcher is not added as a child of the main app. Try adding your listener to the systemManager rather than the main app itself:

this.systemManager.addEventListener("UserInfoEvent", userInfoHandler);
masi

OK, so it looks like everybody was partially right.

Here is the whole solution:

MainApplication.mxml It's important to mention here is that this only works if the service class extends the sprite class, otherwise you can't use the addEventListener method. Looks like another solution for that is to implement the IEventDispatcher

private var service:MyService = new MyService;
this.service.addEventListener(UserInfoEvent.RESULT, userInfoHandler);

UserInfoEvent.as

package events
{
    import flash.events.Event;
    import logic.user.Ego;

    public class UserInfoEvent extends Event
    {
        private var ego:Ego;
        public static var RESULT:String = "resultEvent";

        public function UserInfoEvent(type:String, ego:Ego)
        {
            super(type,  true, false);
            this.ego = ego;
        }

        override public function clone():Event 
        { 
            return new UserInfoEvent(type, ego);
        } 
    }
}

And finally, as already mentioned, the sprite extended MyService.as

public class MyService extends Sprite
{
...
userInfoEvent = new UserInfoEvent(UserInfoEvent.RESULT, ego);
dispatchEvent(userInfoEvent);

so that worked for me. Thanks for all your help guys, I'll mark Adrian's answer as correct since it gave me a direction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!