-
AS3: Using Arrays
Hello all!
Need some help with using arrays, now i have always used for "i" in loops but i have decided to give for each loop a go. I am hoping this will benefit me in a game im working on.
Quick snippet:
Code:
for each (obj in array) {
obj.runFunction();
if (objBoolean=false) {
//This is the bit im confused about... See below
removeChild( obj);
}
}
Using my other method "for i in" loop, i would just check to see what "i" was and then remove that obj in the array and allocate that element empty ready to be recycled. But i was thinking, since the "for each" loop cycles through every element in the array by default, how can i return the number of the element in the array that loop is dealing with. As current with the code i have i think it must remove the whole obj child not just the one it was dealing with since any other code using "obj" stops working on all sprites on stage.
Thanks people...
Aaron
-
Hype over content...
Still have your i var, just increment it.
I use a similar approach,
Code:
var cnt:int=-1;
var baddie:Baddie;
for each(baddie in activeBaddies){
cnt++;
if(baddie.mainloop()=="dead"){
activeBaddies.splice(cnt,1);
replaceToPool(baddie);
}
}
This keeps the array up to date. The only real difference to your code is that the object ( Baddie ) tidies itself up, ie calls it's own houseKeeping() method and that removes it from the stage etc.
Squize.
-
Heli Attack!
2 Things:
1) For each iteration cannot be guaranteed to run 0 -> n, sometimes you will get shuffled iteration, especially on dictionaries.
2)for( ; ; ) is faster than for each()
Just use a for( ; ; ) loop.
-
squize, Cheers for the help. Thats generaly that same way i use with "for i" loop but i was hoping there was just a bit of code that would just know what i was dealing with.
iopred, thanks mate thats some very helpfull infomation i think ill just stick to the normal way. No point waisting precious resources.
Since im here, might as well talk about how i manage elements in an array and if any ones got time tell me if theres a better way of doing it.
Code:
public var objArray:Array;
public var objTotal:int = 1 // used to update loop and with deleting element
public var obj:objClass;
// 0 = No
// 1 = Yes
public var objArray1.empty:int= 1 // used to see if element is empty
public var objCreate:int= 0 // used to create new objects
objArray= new Array();
for (var i:Number = 0; i < objTotal; i++) {
if (objArray[i].empty=1&&objCreate=1) {
obj=new objClass(objX,objY);
objArray[i]=obj;
addChild( obj);
objArray[i].empty=0
} else if (objArray[i].empty=0) {
//Run code for all elements that are used
}
}
So its pretty straight forward. Im only new to this so any help is appreciated.
Cheers every one.
Aaron
-
Senior Member
The line
if (objArray[i].empty = 1 && objCreate = 1) {
is probably intended to check conditions but by using operator "=" you are simply assigning value to variables. Remember, equality condition is not "=", its "==".
-
Hype over content...
"for( ; ; ) is faster than for each()"
Is it ? I'd heard it was quicker if every item in an array was of the same type. If that's the case I'll revert back to while instead ( I don't know how for...each performs on vectors though ).
Squize.
-
Pencil Farmer
Originally Posted by amcotterill
i was hoping there was just a bit of code that would just know what i was dealing with.
You could use indexOf:
PHP Code:
var i:int;
for each(myObj in myArray){
i = myArray.indexOf(myObj);
// do stuff with i...
}
It will return the index value of the current object.
I would assume that doing it this way would be slower than any of the other suggestions here, but I thought I'd throw it out there just so you know about it. It might help solve a future problem.
-
tonypa: Cheers for the reminder mate, i wrote it up just as a demo to post in there but i didnt test it so i missed that.
cadin: Thanks mate that is what i was looking for, couldnt find it in the live docs. But any way if its slower i might as well just avoid it, but its always handy to know.
Thanks every one for the infomation and help. I rekon i've got this sussed.
RESOLVED!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|