Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-16-2012, 04:03 PM   #11
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,397
Default

an Array is just a list of references.
The children on the stage have to be removed using the removeChild() method.

A simple way to do this is with a while loop
ActionScript Code:
// "container" is a Sprite within which are all of your child objects while(container.numChildren > 0) {     container.removeChildAt(0); }
[afz]snickelfitz is offline   Reply With Quote
Old 04-16-2012, 04:19 PM   #12
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

In afz's example, "container" is whatever Sprite or Movieclip you added the children to when you created them. In your original code you tried to do this with "ba" but if "ba" is an Array, it's not part of the display list.

If "container" has children that you don't want to remove because they aren't in the "ba" Array I think you can do this:
Code:
for (var ch:MovieClip  in ba) {
  container.removeChild(ch);
}
rrh is offline   Reply With Quote
Old 04-16-2012, 04:49 PM   #13
TJRobertson86
Registered User
 
Join Date: Apr 2012
Posts: 37
Default

I SEE.... It looks like my problem is I'm not placing the bears in a container, I've just been placing them on the stage (...or randomly off-stage to be specific)

I went ahead and added a container (bearContainer) and changed addChild to bearContainer.addChild; However, it's now causing a lot of problems.

Is there a way to remove the children from the stage, or do I need the in a MovieClip container to remove them?

Thanks
TJ
TJRobertson86 is offline   Reply With Quote
Old 04-16-2012, 04:53 PM   #14
TJRobertson86
Registered User
 
Join Date: Apr 2012
Posts: 37
Default

Also, lauri wrote some code for me, including a removeObject function, which you can see here:

ActionScript Code:
function removeObject(obj:MovieClip, objectArray:Array):void {     // Remove listener.     obj.removeEventListener(Event.ENTER_FRAME, enterFrameHandler, false);     // get array index     var i:int = objectArray.indexOf(obj);     // remove emement from array with index number     objectArray.splice(i, 1);     // remove object from stage     removeChild(getChildByName(obj.name)); }

Is this something I could use to remove all children from the bear array (ba)? I tried to use the function, but I couldn't figure out how to write it.

EDIT:

I think I've thought of a way to remove all the bear children without placing them in a container... however it still isn't working... tell me what you think of this method:

I created a variable called "bearsOnStage" which keeps track of how many bear children are on the stage at any given time (I traced the value, and it works great).

Then I wrote this loop, which uses the removeObject function:

ActionScript Code:
for(var i:uint = 0; i < bearsOnStage; i++) {     removeObject(bear[0], ba); }

I don't get any error messages when I run the swf, however, no bears are removed either.

What am I missing? it's the same line of code I use for when the bears get shot:

ActionScript Code:
removeObject(bear, ba);

It works great when the bears are shot, but it's not doing anything in the loop...

Sorry if i'm missing something really dumb

EDIT:

I just realized that "bear" doesn't refer to the movieclip unless it's inside a function that's receiving a movie clip. Example:

ActionScript Code:
function moveBear(bear:MovieClip):void {     direct(bear, guy1); }

The variable I created for a new bear movieclip child is "nb", but I can't figure out how to reference the child display object inside of a new function...

Last edited by TJRobertson86; 04-16-2012 at 05:50 PM.
TJRobertson86 is offline   Reply With Quote
Old 04-16-2012, 06:52 PM   #15
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,982
Default

If you added them to the stage, then the stage is your container.

Code:
for(var i:uint = 0; i < bearsOnStage; i++)
 {
     removeObject(bear[0], ba);
 }
The trouble here is that "bear" isn't an Array, is it? So using bear[0] doesn't make sense.

Maybe this, since "ba" is an Array that contains bears, right?
removeObject(ba[0], ba);
rrh is offline   Reply With Quote
Old 04-16-2012, 08:03 PM   #16
TJRobertson86
Registered User
 
Join Date: Apr 2012
Posts: 37
Default

You're awesome rrh, that totally worked

I feel foolish for not noticing that :/ I don't know much about actionscript, but I should know that you can't call on the index of a movieclip, lol
TJRobertson86 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:25 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.