Why is my EVENT.ACTIVE running twice?

When I add a listener EVENT.ACTIVATEto my project and then alt-tab and back to my project, it starts twice.

edit: shaunhusain, and I seem to have found the cause of the problem, although without a solution. When starting a standalone player version 11+, the event fires 2x. When starting the standalone version of the player <11 or any version in the browser, it starts 1x. So it looks like there might be a bug in the latest versions of the projector of the flash player. I'm going to nail the exact versions and report it to adobe and see what happens. Thanks to everyone who read this and tried to help!

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

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

This seems to be a common question, but Google has not found anything.

the 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 matter if I add a listener to the scene or something else, the behavior is the same.

The same thing happens with EVENT.DEACTIVATE. Others, such as a mouse, work fine.

My goal is to pause the game on EVENT.DEACTIVATEand disable it on EVENT.ACTIVATE. The problem is that when an event fires twice, it calls a function without repeating, which has undesirable consequences.

+5
source share
2 answers

The ActionScript® 3.0 Reference for the Adobe® Flash® platform talks about this event:

, Flash Player AIR . , , EventDispatcher , . , . DisplayObject.

, ? , , , , ? ? , , , , , , :

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

        trace(e.target);
    }

- , , .

+1

AIR Mobile.

, , "/". , 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;
    }
}

_STATE null, .

Event.ACTIVATE Event.DEACTIVATE , onAppStateChanged , :

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

Switch-Statement, , Robert Penner AS3Signals (GitHub), / .

0

All Articles