0

I have seen many questions on removeChild.

I've got an invaders type game, where bricks are falling from top, and you have to destroy them with a ball that keeps bouncing around.

When I want to remove the brick (referencing it with an array in a for loop), I randomly get the exception error (like many others) that the object must be a child of the caller.

This was a solution:

if (this.parent != null)
{
this.parent.removeChild(this);
}

-- that is in my case, called from within the class of the target object (the brick).

BUT the thing is.. if this.parent really IS null the brick just stays there! (when it should have been removed)

Zeeshan Ahmad
  • 345
  • 3
  • 17
  • 1
    Generally, if you can see an object on the screen it has a parent. Are you sure the object left on screen is one that you tried to remove? You can debug this lots of ways, for example: put a click handler on the objects. When one is left on stage, click the object. In the click handler, see if that object has a parent. Check if the object's `stage` property is not null. Determine if that object is in the array you were iterating over, etc. – Sunil D. Aug 04 '12 at 20:19
  • Again @SunilD. good starting points, using fxspy (if using flex, google it) or monster debugger (works with AS3 alone, a little less intuitive) can be helpful for checking things out at run-time directly interacting with the UI to see properties about an element you see on screen. One possibility I can think of is that the brick is removed from it's parent by some method then it's re-added either to that parent or another display object and your code is executed in the moment that parent is null. – shaunhusain Aug 04 '12 at 20:40
  • Thanks a lot guys. I've solved the issue but am still unsure in understanding... So what I have is, whenever the ball hits a brick, my previous code would remove the brick from its parent and then splice it from the array. But when I commented out the splice on the array, it worked as is should have! ... and even more, I saw in Monster Debugger (thanks @shaunhusain ) that the length of array reduced (I didn't expect that) So my question is : Does removing Sprites from the stage also remove them from the array they were in? (provided that no variable points to the sprite) – Zeeshan Ahmad Aug 04 '12 at 23:12
  • No, removing a Sprite from the stage should not remove it from your array as well. It sounds like some other code may be doing that. – Sunil D. Aug 05 '12 at 00:52
  • Actually, you're right. I know something VERY fishy is happening in my code... I'll get back after investigation. Is it possible to use fxspy with pure as3 code? – Zeeshan Ahmad Aug 05 '12 at 11:04
  • Indeed there was a line of code in my Main class doing the same thing! I really appreciate your help, thank you! – Zeeshan Ahmad Aug 05 '12 at 14:10

1 Answers1

0

Try this for remove child. It removes all children from parent. Also If you want to remove all but save first one, simply change to yourMovieClip.removeChildAt(1);

while(yourMovieClip.numChildren){
    yourMovieClip.removeChildAt(0); 
}
Katax Emperore
  • 465
  • 1
  • 8
  • 29
  • That is a nice suggestion, (I will utilize this some time) but there was some other code in my game giving me problems. Thanks to everyone for help! – Zeeshan Ahmad Aug 10 '12 at 17:27