Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AddChild RemoveChild problems

New Here ,
May 23, 2013 May 23, 2013

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

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2013 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2013 May 23, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2013 May 23, 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"));
                    }
          }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2013 May 25, 2013

Ok, I finally managed to resolve this problem, now when I click on a button, it fades out the current Info if there is one and then it calls an update function on the Info, the Info updates with new data, when this is complete an event gets fired and it fades in again, so I think, working with only one instance of Info is better than removing and adding one every time I click on a button?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2013 May 25, 2013
LATEST

that's not necessarily true but it could easily be true for your situation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines