What does this mean?

3 posts

Flag Post

this.parent.removeChild(this);

The parent bit specificly

 
Flag Post

The parent property of an object is a reference to the instance of DisplayObjectContainer which added the DisplayObject in question to the display list.

ie;

 // instance of World
  ...
  addChild(mountain);

The parent of that instance of Mountain would be the instance of World.

removeChild tells it to remove it from the display list.

 
Flag Post

Every DisplayObject has a .parent property. The parent is the DisplayObject DisplayObjectContainer that added it as its own child.

For instance, say you have a Sprite mysprite and you put a Bitmap mybitmap on it as so:

mysprite.addChild(mybitmap);

Now mybitmap is mysprite’s child and mysprite is mybitmap’s parent. If you call mybitmap.parent it returns the reference to mysprite. Clear so far?

OK, so the line of code you wrote up in the OP simply calls the removeChild() function on the current object’s parent, using the current object as the argument. In plain English, the current object is to be removed from the display list.

Note that the object won’t actually be collected as garbage unless there is no other reference to it anywhere else, so usually you’re better off handling the removal and subsequent cleanup at the parent level.

Edit: Senekis is right, the parent is a DisplayObjectContainer, not just a DisplayObject. Also, ninja’d.