1
votes

I'm working on a mobile AIR project using Flash Builder 4.6. I'm dispatching a custom Event but it is never being heard. I've checked similar questions but cannot find an answer which solves it for me. In my main mxml file I add the listener in the addedToStage event:

[EDIT] adding the listener to the userStatus instance. Still not working.

<s:ViewNavigatorApplication
  ...
  private var userStatus:UserStatus;
  protected function addedToStageHandler(event:Event):void
   {
     userStatus = new UserStatus(); 
     userStatus.addEventListener(CustomEvent.CREDENTIALS_READY, credentialsReadyHandler);

The CustomEvent class:

    public class CustomEvent extends Event
{
    public static const CREDENTIALS_READY:String = "credentialsReady";
    public function CustomEvent(customEventString:String){

        super(customEventString, true, false);
    }
}

The class which dispatches the CustomEvent after a successful service call:

public class UserStatus extends EventDispatcher
{
  ...
    //event handler
    private function userStatusLoaded(e:Event):void 
    {
        var json:Object = JSON.parse(paidUserLoader.data.toString());
        if(json.success == "true")
        {
            trace("UserStatus::this is a paid user!");
            dispatchEvent(new CustomEvent(CustomEvent.CREDENTIALS_READY));
        }

I can see the trace statement in the console, so I know the code is getting there. But the listener in the s:ViewNavigatorApplication class never fires.

Can anyone see why?

Thanks.

1
this.addEventListener(CustomEvent.CREDENTIALS_READY, credentialsReadyHandler); Try replacing this with the name of your instance of the UserStatus class - CyanAngel
@CyanAngel - a good thought. Same result, though -- nothing. - David
You can't listen on a parent if the dispatcher isn't a child of said parent - BadFeelingAboutThis
LDMediaServices as it turns out the UserStatus is not even a DisplayObject so comment does not apply. - BotMaster

1 Answers

2
votes

Any custom class event has to override the clone() method. This method is automatically called internally but if it's not present (overrided) then only direct dispatch can work but events cannot traverse a hierarchy.

EDIT: nvm, there's no reason for your ViewNavigatorApplication to actually catch that event since UserStatus is not a DisplayObject. You are confused about how event are dispatched and are supposed to work. Only the UserStatus itself can catch the event it is dispatching. No other object can. In the case of a display list event propagate but for all non displayobject they need to catch their own dispatch.

EDIT: Answering comment:

There's somewhere a reference of the UserStatus instance, it might be in the ViewNavigatorApplication or not, if it is then:

myuserstatus.addEventListener(CustomEvent.CREDENTIALS_READY, credentialsReadyHandler);

would work. If it's not there then you need to pass along the information from whether class having the reference to UserStatus to the ViewNavigatorApplication. This can be done by redispatching the event (this is where you need to override clone()).

The instance of someotherclass has a reference to userstatus and does listen for the event. When the event fires the someotherclass instance catches and redispatches it. The ViewNavigatorApplication listen for that event directly or via the someotherclass instance reference and finally catches the event.

EDIT: responding to new edits

Let's fix your custom event class. By default you set bubbling to true but only DisplayObject can bubble so it should be set to capture (false)

super(customEventString, false, false);

That should fix it I bet.