Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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());
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
(I hope you get paid for this. )
I was pretty sure you didn't mean 'someParent' literally, but I wasn't sure what to replace it with. There aren't any instance names (assuming you mean the kind you type in under "Properties" tab) as the lights are created/generated through an array.
The code appears it runs fine without anything at the beginning, though, presumably because it doesn't go any "deeper" than "lights", I guess there are no children.
And thanks for the MovieClip(root) bit. I forgot about that.
Copy link to clipboard
Copied
Most of us just answer for fun. Payment is in the form of marking helpful/correct responses .
If you directly added the lights to the main root then there will be no 'someParent' needed, since 'root' is the parent itself. The second loop that works just abstractly didn't care what the parent of the light was by using the parent property of each individual light as it looped, so I'd just go with that one for flexibility.
Careful with 'root'. It's easy but not best practice. I'm only mentioning that when you're just starting out because as you advance as a developer, you most likely will never use it for many reasons I don't want to overwhelm you with now.
You're welcome and good luck!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now