Guig0:
<hr>
Sorry, I should have quoted alethos not you - I was pointing out the similarity of for() and for in() loops.
That said, if you want to reference the property speed which is contained within the object ball then the script
for (var speed in ball){
trace ("speed of the ball is = " + speed);
}
<i>is</i> wrong.
I’m assuming that you’ve created an object (perhaps a movie clip, perhaps something else like an associative array) and you want to reference its properties.
<b>Scenario A - it’s an associative array</b>
ball={speed:27, rotation:6.998, direction:‘west’}
If you want to get the value for speed you’d use
trace("speed of the ball is = "+ball.speed);
using your script:
for (var speed in ball){
trace ("speed of the ball is = " + speed);
}
will return the trace
speed of the ball is = speed
speed of the ball is = rotation
speed of the ball is = direction
and using the corrected script i.e.
for (var speed in ball){
trace ("speed of the ball is = " + ball[speed]);
}
will return
speed of the ball is = 27
speed of the ball is = 6.998
speed of the ball is = west
The point is that the name of the iterant (in this case speed) is <b>completely irrelevant</b> and the script
for (var mashedTurnip in ball){
trace ("speed of the ball is = " + ball[mashedTurnip]);
}
will return exactly the same result
<b>Scenario B - its an object</b>
ball = new Object();
ball.speed=27
if you want to get the value for speed you’d use:
trace("speed of the ball is = "+ball[speed]);
<b>Scenario C - it’s a MC</b>
trace("speed of the ball is = "+instanceName.speed);
alethos:
<hr>
The beauty of for in loops is that you don’t have to enumerate each element by instance name. As I said the iterant can be whateve you like and
for (bakedPotatoes in _root.christmasTree) {
_root.christmasTree[bakedPotatoes].gotoAndStop(“twinkle”);
}
will work just as well. A for in loop iterates through every element - you don’t have to specify them. And (even better) you can use it as a recursive function so if my lights MCs had children called redLight, yellowLight, etc. I could loop through those as well.