Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 433
WgFunstorm Avatar asked Nov 13 '22 04:11

WgFunstorm


1 Answers

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.

like image 200
Łukasz Zaroda Avatar answered Nov 16 '22 05:11

Łukasz Zaroda