Sorry, couldn't resist, had to use AntiPostLimit account, it's ok since it's the original purpose of the anti post limit accounts (like schorhrs)
The one thread to rule them all: AS: Main
This is a useful little thing, again this isn't a long tutorial, it's very basic and it covers a VERY USEFUL OFTEN OVERLOOKED type of looping in flash, the for... in loop
Uses
There are many uses for for in loops:
1)looping over all movieclips in a container
2)looping over all strings on data.
3)looping over all the objects ;)
I will cover:
the instanceof method
for in loops
The actual AS
first of all I'll start with the instance of method, it basically checks if an object is an instance of a class, more advanced talk about classes and objects at AS: OOP.
for example
s.instanceof(String);
will only return true if 's' is a string
_root.mc1.instanceof(MovieClip);
will only return true if 'mc1' is a movieclip
simple eh?
more about instance of
The actual loop
the syntax of a for in loop is:
for(reference in container){
//do actions
}
now the reference part is the one hard to concieve, reference isn't an actual object, it is a reference to all objects. what flash really does is place "links" to all of the objects on the Stage in an array and reference is an array index. more about arrays
the container is the object where the check will be held, for example if _root is the container it will loop over every single thing in your movie, this is why instance of movieclip is importnet
let's look at examples
var counter:Number=0;
for(i in _root){
if(_root[i].instanceof(MovieClip)){
counter++;
}
}
trace(counter);
this counts the number of movieclips in the movie.
remmember container[reference] is always the currently pointed object, in our case _root[i].
very useful, ask any questions