AddChild RemoveChild problems
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