1
votes

I'm asking this question just because curiousity. In as3, while using Event.ADDED_TO_STAGE listener, it's called 2 times automatically. I've tested this with trace. I've checked if I add the movieclip 2 times but there is no such thing. I only add the movieclip once.

If I remove this listener on the begining of the listener's function, this problem doesn't occur. To give an example which traces only once:

addEventListener(Event.ADDED_TO_STAGE,added);

public function added(e:Event){
  removeEventListener(Event.ADDED_TO_STAGE,added);
  trace("MovieClip is added");
}

And this is the example which traces twice: addEventListener(Event.ADDED_TO_STAGE,added);

public function added(e:Event){
  trace("MovieClip is added");
}

If anyone tells the reason It'd be really appreciated.

Thank you

-Ozan

1
What is the eventPhase and target of the event e during each call?Cameron
e.eventPhase is 2 (each time it's the same) and target is a movieclip called TileManager. (it's this movieclip's parent). I've read something about this problem. They say it's called twice because "added_to_stage" event listener is triggered both when movieclip is added to stage and added to another movieclip. So while adding this movieclip into another movieclip which is on the stage, I undirectly add it into both movieclip and stage. So it's called twice. But when I trace e.target, both return the same value and not the stage.Ozan Deniz
I think you just answered your own question ;-) Looks like it's simply yet another corner case of Flash.Cameron

1 Answers

0
votes

I had the same problem.

How I solved:

I put my load command at the ADDED_TO_STAGE event.

Like this:

    var mcLoader:MovieClipSWFLoader = new MovieClipSWFLoader();

    public function init():void
    {                       
        this.addEventListener(Event.ADDED_TO_STAGE, selfLoad);

        mcLoader.x = 10;
        mcLoader.y = 10;            
        //THIS is important, because when you load, it is automatically added
        mcLoader.autoLoad = false;
        mcLoader.source = "mySwf.swf";
    }

    private function selfLoad(e:Event = null):void
    {
        this.addEventListener(Event.COMPLETE, setLoaded);
        this.addEventListener(IOErrorEvent.IO_ERROR, loadingErrorHandler);

        mcLoader.load();
    }

It worked for me.