I have an array I am using to hold movie clips, and I’d like to be able to change all of them at once using the array. Now, I can do it using array[0,1,2,3,4,5,6,7,8...].changestuff but with 60 or so movieclips, it gets unwieldy. Is there a simpler way to go through the entire array and apply the changes to each movieclip stored within?
I have an array I am using to hold movie clips, and I’d like
Share
If you have an array of MovieClips something like this:
You have 3 ways of looping through it and affecting all the MovieClips inside it. The first is the basic for loop:
The basic for loop is the quickest way of looping through an array. You just need to find the MovieClip at the specific index using the brackets operator.
a[i]will return the MovieClip at the current index (e.g.a[0]will return the MovieClip at the start of the array,a[1]the next one, etc).You can also do a for..each:
For..each loops are slightly slower than a normal for loop, but has the added benefit of having access to the object directly, already casted. It can be quite convenient
And finally use the
forEach()method:I’ve never really seen
forEach()used anywhere, but you might find it useful. For the callback, you need to declare 3 parameters, the object, the index, and the array itself.You can find more info at the Array documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html