"For...In" statements

I am having a hard time understanding for…in loops. I have seen David use it in his signature, and I understand what the loops is doing, but only from the context of the rest of the signature. If somebody could explain how the for…in loop works, I’d be greatful. Here’s what I know:


for (proprtyThing in objectThing) {
  trace (proprtyThing);
}

My understanding of this loop tells me that if I run it, I’ll get a list of all the properties of the object named objectThing…but this makes no sense to me!!

A for loop is similar to a while loop, but with a more compact syntax

Syntax:

for (initialization; condition; update){
substatments
}

**Example:**for-in loop

for (var speed in ball){
trace ("speed of the ball is = " + speed);
}

But why can’t you just say:


trace("Speed of the ball = " + ball.speed);

That seems a lot more compact to me. Am I missing something?

For in allows you to apply a multiple reference to a declared property in nested MC instances and is an absolute godsend for gaming apps

Suppose I have a christmas tree mc that contains 100 fairy lights mcs nested within it and i wan to to tell all of these lights to gotoAndPlay(“twinkle”)

I’d use the syntax

for (lights in _root.christmasTree) {
_root.christmasTree[lights].gotoAndStop(“twinkle”);
}

The iterant - in this case lights - can be anything you like, spinachAndWatercress, thursday, a variable.

And the best bit is that if you add 600 more lights to your christmas tree it’ll work just the same

I’m sorry guig0 but you’re wrong.

And Al, if you want some more explanations, there’s something in the AS tricks about it.

pom :slight_smile:

WRONG??? Well… probably:)

but, how wrong:q:

*Originally posted by Guig0 *
for (initialization; condition; update){
substatments
}

A for in loop uses an iterant to loop through elements in an array. In my example above it was nested MCs but it could have been anything. Also while it does iterate through each sequential object, you don’t have to specify a break clause or initialise and increment a starting variable.

I was replying to the question on same bases:

*Originally posted by alethos *


for (proprtyThing in objectThing) {
  trace (proprtyThing);
}

I dont never said that for has only this aplication. Got it?

=)

Ok, Guigo’s For…Next loop is valid, but it’s not what I was looking for. I know how to use a For…Next loop (i.e. for(i=0;i<5;i++){blah}), but what I was after was the For…In loop.

Jsk, your two explanations have cleared things up a bit, but not fully. First I need some clarification on the tree example. Specifically, I’d like to know what the “light bulb” MC’s are called. From what I see, you’re saying christmasTree is an MC, but then you call the lights by using the tree as an array (christmasTree[0], [1], etc.). If my light bulb MC’s are called “lightBulb1, 2, etc”, then do I substitute “light” in your code with “lightbulb”? Or can I equally well put “i” instead of “light”?

It seems the key to my understanding is figuring out whether the christmasTree[variable] is read as christmasTree[0], [1], etc OR read as christmasTree.light1, .light2, etc. (assuming your MC’s are called light1, 2, etc. If you answer this simple question I think I’ll be totally clear. :slight_smile:

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.

Beautifully explained, thank you.

My only comment for others that might be reading this thread is that I thought associative arrays in Flash ARE just instances of the Object() object.

So

ball={speed:27, rotation:6.998, direction:‘west’}

and

ball = new Object();
ball.speed=27;
ball.rotation=6.998;
ball.direction=‘west’;

are the same thing, right?

[EDIT] now I got it! sorry!

=)

Al>> Yes, they’re the same.
Jsk>> That was very well explained indeed :slight_smile: