Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-16-2012, 09:33 PM   #1
Brandondspicer
Registered User
 
Join Date: May 2012
Posts: 42
Default How to remove all child(children?/objects) in array

I would say its a simple question but I've researched everywhere and the only thing I found that might work is this

while (container.numChildren > 0){
container.removeChildAt(0);
}

But when I attempt to use this it won't work.
The name of the array is army
So my current attempt is to instead of removing all children, to remove a certain child like so

If (enemy.hitTestObject(avatar)){enemy.parent.removeC hild(enemy)}

This actually Did remove the specific child but, it caused glitching(objects above the removed child stopped moving until mouse was moved, objects below continued moving) and this error:
TypeError: Error #1009:Cannot access a property or method of a null object reference. at AvoiderGame/onTick()
at flash. utils: :Timer/_timerDispatch()
at flash.utils: :Timer/tick()

So for my final attempt to remove child correctly I placed the code inside the enemy class file(too complicated to explain ATM)
Brandondspicer is offline   Reply With Quote
Old 05-16-2012, 09:41 PM   #2
dmb85
Senior Member
 
Join Date: Jan 2012
Posts: 608
Default

ActionScript Code:
while(army.length > 0) {    army.pop(); }

Or probably the faster way:

ActionScript Code:
army = new Array();
dmb85 is offline   Reply With Quote
Old 05-16-2012, 09:45 PM   #3
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

If you want to remove everything from array, then
ActionScript Code:
array.length = 0;
is the fastest way. If you want to remove a number of elements (but leave some)
ActionScript Code:
array.splice(position, deleteElementsCount, other, elements, to, insert);
The reason it works like that with numChildren is that it's not an array - it's a list and removing from the head of the list is the fastest way to remove elements one by one, unless you can remove the entire list.

EDIT: Stop yourself when you do anything.parent.removeChild(anything) and ask - what if anything was already removed - would it have a parent?
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it

Last edited by wvxvw; 05-16-2012 at 09:49 PM.
wvxvw is offline   Reply With Quote
Old 05-16-2012, 10:22 PM   #4
Brandondspicer
Registered User
 
Join Date: May 2012
Posts: 42
Unhappy

Ok i really appreciate these answers, they give me alot of ideas.
Im going to ask a new question though.
1. Now that ive removed these from the array, how do i remove them all from the stage?
my idea was:
Code:
 if (enemy //--is not in array){enemy.parent.removeChild(//--enemies not in array)}
Sorry it would take alot of research to fill in with code.
2. Ill look up the array.splice() syntax, but how would i specify the child that was hitTested.
3. how would i link the enemy and avatar in another .as file(sorry if the question is explained incorrectly(probably is))
Code:
package 
{
	import flash.display.MovieClip;
	public class Enemy extends MovieClip
	
	
	{
		public var enemy:Enemy; //---doesnt "link"
		public var avatar:Avatar; //---doesnt "link"		
		public function Enemy(startX:Number, startY:Number)
		{
			
			x = startX;
			y = startY;
if(enemy.hitTestObject(avatar){enemy.parent.removeChild(enemy)
			
		}

		public function moveDownABit():void
		{
			y = y + 3;
			
			
		}
	}
}

Last edited by Brandondspicer; 05-16-2012 at 10:32 PM.
Brandondspicer is offline   Reply With Quote
Old 05-16-2012, 10:56 PM   #5
Brandondspicer
Registered User
 
Join Date: May 2012
Posts: 42
Default

Ok this is somewhat irrelevant to my first questions
If I don't use class files(.as files) will it cripple or make my carrer in as3 difficult?
The whole reason I decided to use .as files is because I couldn't create two of the same exact movieclips(like addchild(bluecircle) would just move the child instead of creating a new one?) Sorry If I shouldn't double post
Brandondspicer is offline   Reply With Quote
Old 05-17-2012, 12:39 AM   #6
tango88
FlashByNight.com
 
tango88's Avatar
 
Join Date: Aug 2006
Posts: 289
Default

>>If I don't use class files(.as files) will it cripple or make my carrer in as3 difficult?

Career as in you'll be looking for a job as an Actionscript programmer? Yes, certain production houses will insist you use class files. This is to help separate the job of programmer and graphic artist. Plus you basically want to be as knowledgeable about Flash as possible. But take it slowly... step by step.


>>The whole reason I decided to use .as files is because I couldn't create two of the same exact movieclips(like addchild(bluecircle) would just move the child instead of creating a new one?)

Hmm? That doesn't really make sense.


>>Sorry If I shouldn't double post

That's okay, but why not repost this as a separate question?
__________________
Games and more:
Flashbynight.com | Tutes | Twitter
tango88 is offline   Reply With Quote
Old 05-17-2012, 12:44 AM   #7
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

Array is a data structure with certain properties, like it has length (the number of elements it contains), it can tell the position of every element and it promises that the time required to access an element, given you know it's position is "immediate".
Quite by chance there is a class in AS3 called Array - strictly speaking it doesn't exactly satisfy the description of what arrays should be, but it is close enough.

In AS3 you create arrays by either using a literal initializer of the form:
[element_0, element_1, ... element_i] - which creates an element of the length i containing elements element_0 through element_i.
Alternatively, you can use new Array(i) to create an empty array of i length.

There are less often used ways of creating arrays: new Array(element_0, element_1, ... element_i); is essentially the same as the first one described (the details of the difference aren't really important for now). Or, you could even write Array() - and it, too, will create a new array - but this is confusing syntax-wise, and you probably should avoid doing it.

When you call addChild() or removeChild() you tell the display object container, which owns these methods to add or remove children. Eventually, the container may choose to store children in an array, but, in reality this is not what happens, besides, you don't really have a way to know how exactly the children are stored, and, ideally, it shouldn't even concern you (unless in the corner cases, like that when removing many children at once - it's useful to know that removing from the head is faster then from the tail).


Re' your other question... text files so far have been the best medium for writing and storing the code. Some modern code editors try to change that, transforming it into a sort of MS Excel tables... which I personally see as a waste and a total misconception, but wouldn't like to start a flame war.
Writing code is a different kind of activity (different from graphic design) - it is in it's nature more similar to the work of a writer or a scientist - in both cases, it's natural to have it written or typed. It's rather uncommon to do it in another way. Flash is quite particular in this sense because it tries to combine the process of writing the code with the visual graphic tool. Probably, you could take advantage of both worlds, programming and designing, but most commonly you specialize on one side of it.
However, if you intend to be a programmer, AS3 or anything else, then writing your code in text files (in the context of Flash) is the way to go, because it opens the door to better code editors, but not only that, it also trains you to work in the environment typical for a programmer - which is often suited for more then a single programming language. Things like source control (versioning) systems, organizers of all sorts, code statistics and many other useful programming tools rely on that the text of the program is easily accessible as text files.
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it

Last edited by wvxvw; 05-17-2012 at 12:48 AM.
wvxvw is offline   Reply With Quote
Old 05-17-2012, 01:18 AM   #8
Brandondspicer
Registered User
 
Join Date: May 2012
Posts: 42
Default

I just want you to know wvxvw you really helped me out, this whole time I've been trying to jump right into creating a game. Instead of you solving my problem you helped me to understand the "element"(is that the right word o.o)array. This was truly an inspiring post I plan to go back to reading books and studying other peoples codes. Of course hands on experiance beats knowledge(sometimes) but im beggining to realize how complicated actionscript(even flash) is! I guess it's called a language for a reason. I hope that you continue inspiring other developers for you have truly gave me inspiration THANKS
Brandondspicer is offline   Reply With Quote
Old 05-17-2012, 01:26 AM   #9
belonzion
Registered User
 
Join Date: May 2012
Posts: 2
Default

Hi. I'm new here so, maybe lot of newbie question coming...
I'm doing a "game" where you type how many pair of frogs you want in the game and you need pass all frogs from left to right and vice-versa.
So i create 2 Arrays and a function where they add this frogs in stage and add in the Array.

ActionScript Code:
import flash.events.MouseEvent; import flash.events.Event; import flash.display.Stage; import flash.display.MovieClip; var sapo_verde:Array; // frogs from right var sapo_rosa:Array; //frogs from left function cria_sapos(event:Event):void {     var texto:String = valor.text;     var num = Number(texto); // get the number of frog's pair     sapo_verde = new Array  ;     sapo_rosa = new Array  ;     for (var i:Number=1; i<=num; i++)     {         var stg:Number = stage.stageWidth;         var rosa:ran=new ran();         var verde:sapo=new sapo();         rosa.x = (stg/2)-(100 * i);         verde.x = (stg/2)+(100*i);         verde.scaleX = -1;         rosa.y = 200;         verde.y = rosa.y;         addChild(rosa);         addChild(verde);         sapo_verde.push(verde);         sapo_rosa.push(rosa);     } } butao.addEventListener(MouseEvent.CLICK,cria_sapos);

The problem is : How i delete the frogs from stage? Like... if i choose create 5 pairs and later create 4, still remain 5 in screen. Dont know if i create another function after the loop with a condition and removeChild. Tried that and not remove the frogs.
belonzion is offline   Reply With Quote
Old 05-17-2012, 06:40 AM   #10
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

Glad I could help, Brandondspicer! Yup, an element of an array or a memeber of an array is a common way to say that.

belonzion, if I understand you right, you want to do something like:

ActionScript Code:
var elementToRemove:DisplayObject = sapo_verde.splice(i, 1)[0]; // you'd need a way of knowing what to delete - I'm not sure what's the game rules are. removeChild(elementToRemove);
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw 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 04:39 AM.

///
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.