Skip to main content
Inspiring
January 30, 2014
Question

Remove all movieclips associated with an array?

  • January 30, 2014
  • 1 reply
  • 885 views

Hi, I'm going through Gary Rosenzweig's AS3 book and am having a hard time with even simple things.

Right now I'm trying to have 5 different movieclips associated with array "lights" removed from the screen when the game is over. I'm able to do removeChild(lights[0]); and remove one item, but if I try to select more than one - like removeChild(lights[0,1]); - I get a really weird and big error message, one I've never gotten before.

Any ideas? I'll upload the FLA and AS files - https://www.mediafire.com/folder/hiksiia0pl4lm/memoryGame

This topic has been closed for replies.

1 reply

sinious
Legend
January 30, 2014

That's why they call it 'removeChild()', singular. It will remove one child at a time.

If this isn't going on mobile and you can wait for reference collection to dump then you can simply get rid of the parent. Otherwise remove them in a simple loop:

while (lights.length > 0) someParent.removeChild(lights.shift());

An Array's shift() method will return the object at position index 0, hand it to removeChild() which will remove it from the parent and Array at the same time. The loop will continue until the Array lights is empty (and children removed).

If each light is in a different parent, something like:

while (lights.length > 0) DisplayObject(lights[0]).parent.removeChild(lights.shift());

tytbone2Author
Inspiring
January 30, 2014

Thanks, that second option works.

I do have another question: I have my code/class inside a movieclip, so that any "gotoAndPlay"-type functions run in the context of that movieclip and not on the main timeline.

If I want the code to jump to a new frame on the main timeline, what can I write to have this happen? "stage.gotoAndPlay(3)" and "parent.gotoAndPlay(3)" don't seem to work as I guess I'm referencing a static item through a function or whatever the appropriate terminology is. Suggestions?

sinious
Legend
January 30, 2014

Note that 'someParent' as I used literally was meant to be replaced by the actual name of the parents instance. It's not a keyword in ActionScript. If your code is running on the same timeline as the parent and you run that code, replacing 'someParent' with the proper instance name, it will work, as long as that parent contains all the light Array objects.

To explain why your 2 code attempts errored, on the stage attempt you want the main MovieClip instance of the stage, or 'root', so it would be MovieClip(root).gotoAndPlay(3). The Stage object is special and doesn't have a timeline. A child MovieClip (root) is the master container which has a timeline and contains all children.

On the parent attempt, you need to specify the context of the hierarchy. If the timeline is just one level above the script you're writing then start at 'this' to be specific, then use parent, and cast for safety: MovieClip(this.parent).gotoAndPlay(3);