WgFunstorm
WgFunstorm

Reputation: 148

Why does my EVENT.ACTIVE trigger twice?

When I add an EVENT.ACTIVATE listener to my project, and then alt-tab away and back to my project it triggers twice.

edit: shaunhusain and I seem to have found the cause of the problem, although without a solution. When running the standalone player version 11+ the event triggers 2x. When running standalone player version <11 or any version in the browser it triggers 1x. So it appears there may be a bug in recent versions of the flash player projector. I'm going to nail down the exact versions and report it to adobe and see what happens. Thanks to anyone who read this and tried to help!!

I want it to fire every time I change focus, I just don't want it to fire twice every time I change focus.

Why is this? Am I doing something wrong? What's the best way to prevent this behavior?

It seems like it would be a common question, but Google turned up nothing.

Code:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            stage.addEventListener(Event.ACTIVATE, test);
        }

        private function test(e:Event):void
        {
            trace(e.target);
        }

    }

}

Actual result:

[object Stage]
[object Stage]

Desired result:

[object Stage]

It doesn't seem to make a difference whether I add the listener to the stage or anything else, the behavior is the same.

The same thing also happens with EVENT.DEACTIVATE. Others such as mouse up work fine.

My goal is to pause a game on EVENT.DEACTIVATE and unpause it on EVENT.ACTIVATE. The problem is that when the event fires twice, it calls the unpause function twice which has unwanted consequences.

Upvotes: 5

Views: 2079

Answers (2)

chamberlainpi
chamberlainpi

Reputation: 5241

I've had the same issue in my AIR Mobile app.

To correct this issue, I've stored the last event name triggered for an Activate / Deactivate event. If it is attempted twice in a row, it just gets skipped with a return;

private static function onAppStateChanged(e:Event):void {
    if (_STATE == e.type) {
        return;
    }

    _STATE =    e.type;
    switch(_STATE) {
        case Event.ACTIVATE: whenActivated.dispatch(); break;
        case Event.DEACTIVATE: whenDeactivated.dispatch(); break;
    }
}

At first, the value of _STATE begins with null, so that should allow it to pass through the first time around.

I bind both Event.ACTIVATE and Event.DEACTIVATE to the same listener, onAppStateChanged in my App's initialization method, like so:

_STAGE.addEventListener(Event.ACTIVATE, onAppStateChanged);
_STAGE.addEventListener(Event.DEACTIVATE, onAppStateChanged);

You can replace the Switch-Statement however you like, I just personally prefer using Robert Penner's AS3Signals (GitHub) to broadcast the resume/suspend signal across the App.

Upvotes: 0

Łukasz Zaroda
Łukasz Zaroda

Reputation: 767

ActionScript® 3.0 Reference for the Adobe® Flash® Platform says about this event:

Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. This event is a broadcast event, which means that it is dispatched by all EventDispatcher objects with a listener registered for this event. For more information about broadcast events, see the DisplayObject class.

For me it looks like you want to prevent its designed behavior? I suppose it was designed to fire every time you change focus, or am I wrong? What do you want to accomplish? However this is an answer, so based on what you wrote - you can do a workaround by just removing a listener after he fired once:

    private function test(e:Event):void
    {
        stage.removeEventListener(Event.ACTIVATE, test);

        trace(e.target);
    }

But I would recommend you to write something more about why are you using it and what want to accomplish if this is not satisfactory.

Upvotes: 1

Related Questions