Skip to main content
vlceversa
Known Participant
May 23, 2013
Question

AddChild RemoveChild problems

  • May 23, 2013
  • 1 reply
  • 1231 views

Ok,  I'm really confused how I should handle this. I have a Main Stage, to this I add several buttons, on a button click it adds a Info Sprite to the Stage with some info about the selected button. On this Info Sprite there is a close button that sends an event to the Main Stage to remove the Info Sprite from the Stage, this works.  My problem is, when I click on another button while the first Info is still displayed, it adds another Info to the Stage. So I have tried to name the Info Sprite and used getChildByName() and added removeChild(getChildByName()) to the button click, but this doesnt work for the first Info, it throws an error that the object is null. What I want, is not adding another Info to the Stage, I want the Info to be updated with the new data.

Simplified:

Stage -> Button Click -> addChild(Info)

Info -> addChild(container) -> click close Button -> send Event to Stage -> Stage removeChild(info)

package  {

 

          public class Test extends Sprite {

 

                    var info:Sprite;

 

                    public function Test() {

                              info = new Sprite();

                              addEventListener("myEvent", removeInfo);

                              addEventListener(MouseEvent.CLICK, buttonClicked);

                              // imagine several Buttons were added

                    }

                    private function removeInfo(e:Event) {

                              removeChild(info);

                    }

 

                    private function buttonClicked(e:MouseEvent) {

                              // remove previously added info

                              addChild(info);

                    }

          }

}

package  {

 

          public class Info extends Sprite {

 

                    var container:Sprite;

 

                    public function Info() {

                              container = new Sprite();

                              addChild(container);

                              // add other things to the container ...

                    }

                    private function closeButtonClick() {

                              // some method to clear the container contents?

                              dispatchEvent(new Event("myEvent"));

                    }

          }

}

I hope that this helps

Regards

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 23, 2013

info should be an Info instance, not a sprite instance and, instead of removing info, you should remove the instance in Info or remove the event dispatcher's currentTarget's parent (assuming the close button is a child of the Info instance and calls closeButtonClick).

vlceversa
vlceversaAuthor
Known Participant
May 24, 2013

So you mean something like this

package  {

 

          public class Test extends Sprite {

 

                    var info:Sprite;

 

                    public function Test() {

                              info = new Sprite();

                              addEventListener("myEvent", removeInfo);

                              addEventListener(MouseEvent.CLICK, buttonClicked);

                              // imagine several Buttons were added

                    }

                    private function removeInfo(e:Event) {

                              removeChild(e.currentTarget.parent);

                    }

 

                    private function buttonClicked(e:MouseEvent) {

                              // remove previously added info

                              addChild(info);

                    }

          }

}

package  {

 

          public class Info {

 

                    var container:Sprite;

 

                    public function Info() {

                              container = new Sprite();

                              addChild(container);

                              // add other things to the container ...

                    }

                    private function closeButtonClick() {

                              // some method to clear the container contents?

                              dispatchEvent(new Event("myEvent"));

                    }

          }

}

While I was at work, I was thinking, is it possible, that I add the Info to the Test instance and just hide it, and in the Info Class I create a function update which sets the approperiate things and then on a button click in the Test Class I just call this update function, maybe with a fade out -> in effect. I think it is possible to get an element by its name in AS3 so it should be easy to set the elements text for example to something new. This would create only one Info instance, and every time I click on a button it just updates?

Regards

kglad
Community Expert
Community Expert
May 24, 2013

try:

package  {
 
          public class Test extends Sprite {
 
                    var info:Info;
 
                    public function Test() {
                              info = new Info();
                              info.addEventListener("myEvent", removeInfo);
                              addEventListener(MouseEvent.CLICK, buttonClicked);
                              // imagine several Buttons were added
                    }


                    private function removeInfo(e:Event) {
                              removeChild(e.currentTarget);
                    }
 
                    private function buttonClicked(e:MouseEvent) {
                              // remove previously added info
                              addChild(info);
                    }
          }
}


package  {
 
          public class Info {
 
                    var container:Sprite;
 
                    public function Info() {
                              container = new Sprite();
                              addChild(container);
                              // add other things to the container ...
                    }


                    private function closeButtonClick() {
                              // some method to clear the container contents?
                              dispatchEvent(new Event("myEvent"));
                    }
          }
}