A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: AS3: Using Arrays

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    28

    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

  2. #2
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    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.

  3. #3
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    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.
    Christopher Rhodes
    squarecircleco.

  4. #4
    Junior Member
    Join Date
    Nov 2009
    Posts
    28
    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

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    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 "==".

  6. #6
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    "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.

  7. #7
    Pencil Farmer cadin's Avatar
    Join Date
    Jul 2006
    Location
    Vancouver BC
    Posts
    323
    Quote Originally Posted by amcotterill View Post
    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){
       
    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.

  8. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    28
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center