PDA

View Full Version : For In Loop for an Array of classes


elveatles
08-05-2008, 09:04 PM
Hello,
I'm trying to figure out how I can use a for in loop to change all the boolean values of many instances of a custom class all at the same time.


for(var i:Boid in boidArray)
{
i.go = goBool;
}


With this little bit of code, the goBool would equal true onMouseDown and false onMouseUp. So in turn, all the instances of the Boid class would be true onMouseDown and false onMouseUp.
Instead it gives me the error 1067:Implicit coercion of a value of type string to an unrelated type Boid.
The Boid class already exists and so does the "go" boolean. What are your thoughts?

Sekhar
08-05-2008, 09:29 PM
The for loop will iterate through index, not values - for that you need for each. Either of these will work:

for (var i in boidArray)
{
boldArray[i].go = goBool;
}

for each (var i:Boid in boidArray)
{
i.go = goBool;
}

elveatles
08-05-2008, 10:41 PM
Thank you very much, it worked!