Skip to main content
Inspiring
October 31, 2013
Question

How to removechild IF it's visible

  • October 31, 2013
  • 1 reply
  • 488 views

I'm making a game in AS3 when you can save and restore your game.

The player can load his saved game at the introscreen OR during the game.

I've got an error (not very important as the game is still working but I'd like to correct in order to be clean) as I don't know how to put it in code.

So, when the player click on "load" at the Introscreen, a window opens where he can choose wich games he wants to restore.

When he choose a game, he click on it and the IntroScreen AND the restore window disappear. Here's the code for this :

if(allSaveData){

..

dispatchEvent(new Event("closeThis"));

this.parent.removeChild(introScreen);

}

But when he's in the game and he wants to restore. He clicks on "esc" and the restore windows appears. But the IntroScreen IS NOT HERE. So when he restore his game, I've got an error with "this.parent.removeChild(introScreen);" as IntroScreen is not here.

I've tried this :

if(allSaveData){

..

dispatchEvent(new Event("closeThis")); }

if (introScreen.visible == true){

this.parent.removeChild(introScreen);

}

But it's not working. Do you know how I can tell the code "if introscreen is here close it, if not don't" ?

Thank you for your help !

This topic has been closed for replies.

1 reply

Inspiring
October 31, 2013

if(this.parent.introScreen != null){

   this.parent.removeChild(introScreen);

}

else{

  //do nothing

}

Inspiring
October 31, 2013

Thanks !

But someone told me an other solution that's working :

if (introScreen.parent) {
    introScreen
.parent.removeChild(introScreen);
}