AddedToStage called twice

myrddin's Avatar

myrddin

08 Aug, 2011 08:27 AM

Hi,

I have a problem, and I don't understand the cause.
I attached a sample project, with FDT.

I have a Context, an InitSiteCommand (called at startup) wich adds a view (Home). In HomeMediator, on "onRegister", I add a view (Player) and I'm listening for the event AddedToStage. But, the last event it's called twice ! You can see tracelog...

If someone could help me ? Maybe I do something wrong or it's a robotlegs issue. I used to work with Robotlegs for a while yet.

Thanks you.

  1. Support Staff 1 Posted by Ondina D.F. on 08 Aug, 2011 05:58 PM

    Ondina D.F.'s Avatar

    Hi myrddin

    You forgot to attach your code:)

    See this thread
    http://knowledge.robotlegs.org/discussions/questions/253-understand...

    and look at Shaun’s answers.

    Without seeing your code, I would say to remove the listener like this:

    public function SomeView()//constructor
    {
    this.addEventListener(Event.ADDED_TO_STAGE, onAddedToTheStage,false,0,false);
    }

    public function onAddedToTheStage(event:Event):void
    {

    this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToTheStage);
    doSomething();
    }

    If the view has a Mediator, you can doSomething() in its onRegister by calling view.doSomething()

    If this doesn’t solve your problem, please attach your code or just paste a snippet.

    Ondina

  2. 2 Posted by myrddin on 09 Aug, 2011 08:18 AM

    myrddin's Avatar

    Hi Ondina,

    Sorry, It seems to me to have attached it...
    For my problem, of course I can remove the listener but it's not normal that it's called twice... furthermore in an strange order.

    Thanks,

    myrddin

  3. Support Staff 3 Posted by Ondina D.F. on 09 Aug, 2011 12:01 PM

    Ondina D.F.'s Avatar

    Hi myrddin,

    For my problem, of course I can remove the listener but it's not normal that it's called twice... furthermore in an strange order.

    I can’t give you an accurate explanation of the event’s capture, target and bubble phases and how it traverses the display object tree up and down and the exact order of execution. It is beyond my level of expertise.
    You’ll find lots of articles about this topic on the internet(s) ;)

    “Event.ADDED_TO_STAGE fires twice” has been reported as a bug http://bugs.adobe.com/jira/browse/FP-1569 (Flash Player 10 - 10_0_12_36), that appears to be fixed, but it isn’t.
    I have 10,3,181,34 installed. I haven’t tried it with newer versions of the flash player.

    So my workaround was correct. Use removeEventListener.

    If you try it without robotlegs, you’ll see that the behaviour is different depending on where you place addChild(new Player()).
    If you place it in Home’s constructor the ADDED_TO_STAGE fires once. If you place it in Home’s onAddedToStage ADDED_TO_STAGE will fire twice.
    With robotlegs, placing the addChild() in the onRegister() is the same as having it in Home.onaddedToStage().

    
    public class Main extends Sprite
    {
        public function Main()
        {
            addChild(new Home());
        }
    }
    
    public class Home extends Sprite
    {
        public function Home()
        {
            trace("Home.Home()");
            //addChildren();
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
    
        private var _videoPlayer:Player;
    
        private function onAddedToStage(event:Event):void
        {
            //removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            trace("Home addedToStage ");
            addChildren();
        }
    
        private function addChildren():void
        {
            trace("Home.addChildren(event)");
            _videoPlayer=new Player();
            this.addChild(_videoPlayer);
        }
    }
    
    public class Player extends Sprite
    {
        public function Player()
        {
            trace("Player.Player() ");
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
    
        private function onAddedToStage(event:Event):void
        {
            //removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            trace("Player ----addedToStage ");
        }
    }
    
    

    Would it be an acceptable solution for you to use Event.ADDED instead of Event.ADDED_TO_STAGE?

    Ondina

  4. Ondina D.F. closed this discussion on 02 Nov, 2011 03:42 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac