PDA

View Full Version : For..In Different in AS3, why?



Blommestein
June 1st, 2007, 05:01 PM
Open a new AS2 document and add this on frame 1:


var ar:Array =[3,2,4,5]
for (var mop in ar)
{
trace(ar[mop]);
}
//ar.push(3); If you wanted to make ar different.
for (var mop in ar)
{
trace(ar[mop]);
}

Works fine. Now open an AS3 document, and do the same. You get an error. Shouldn't 'mop' have gone out of scope? And why comes the error? Is it because mop is trying to be defined twice because it didn't go out of scope? If so, what's a solution for that, without defining two different variables, since delete mop does not work?

Stratification
June 1st, 2007, 05:03 PM
For loops (actually loops in general) don't actually have their own scope in ActionScript. It was the same way in AS2, you just didn't get a warning.

Blommestein
June 1st, 2007, 05:08 PM
For loops (actually loops in general) don't actually have their own scope in ActionScript. It was the same way in AS2, you just didn't get a warning.

Hadn't known that, thanks. I guess the best thing would be to simply reuse it, rather than setting it to null and creating anew variable, waiting for garbage collect to rid of it, right?

Stratification
June 1st, 2007, 05:09 PM
Yes, as long as you're careful to reset the value, just re-using should work great.

Krilnon
June 1st, 2007, 05:10 PM
The for loop even resets it for you.

Stratification
June 1st, 2007, 05:14 PM
That's true, in this case it does. I forgot we were talking for...in rather than a normal for loop.

TheCanadian
June 2nd, 2007, 12:18 AM
This works too (since you're only declaring the variable once):

var ar:Array = [3,2,4,5];
var mop:String;
for (mop in ar) {
trace(ar[mop]);
}
for (mop in ar) {
trace(ar[mop]);
}

Blommestein
June 3rd, 2007, 09:46 PM
Alright hopefully you know why this is working differently for me in AS2 vs 3:

Open a new AS2 document, and add any movieclip to the library with the linkage name 'childclip'.

Now add this on frame 1:


var holder:MovieClip = _root.createEmptyMovieClip("hold",0);
holder.attachMovie("childclip","child1",1);
holder.attachMovie("childclip","child2",2);
for(var tmp in holder)
{
trace(tmp);
}

Now run it and you should get this output:

child2
child1

Now open a new AS3 document, and add this on frame 1:


var holder:MovieClip = new MovieClip();
var child1:MovieClip = new MovieClip();
var child2:MovieClip = new MovieClip();
holder.addChild(child1);
holder.addChild(child2);
addChild(holder);
for(var tmp in holder)
{
trace(tmp);
}

Now run it and you should get... no output.

Any ideas?

TheCanadian
June 3rd, 2007, 10:15 PM
Adding a DisplayObject as a child of a DisplayObjectContainer doesn't make it a property of the parent.

var holder:MovieClip = new MovieClip();
holder.child1 = new MovieClip();
holder.child2 = new MovieClip();
holder.addChild(holder.child1);
holder.addChild(holder.child2);
addChild(holder);
for(var tmp in holder)
{
trace(tmp); //child1 //child2
}

Blommestein
June 3rd, 2007, 10:46 PM
Ah alright thank you. Is there a way to go through every DisplayObject that is a child of a certain parent, and do something to it, then? Without declaring it like that?

If not (though I hope there is a way) what would be a way to do


...
var mc = "a1";
holder.mc = new MovieClip(); // So that it's like saying holder.a1 = new MovieClip();

TheCanadian
June 4th, 2007, 12:37 AM
for(var i:uint = 0; i < myDisplayObjectContainer.numChildren; i++) {
trace(myDisplayObjectContainer.getChildAt(i));
}

var mc:String = "a1";
holder[mc] = new MovieClip();

Blommestein
June 4th, 2007, 01:00 AM
Holy ****, thank you that's perfect.

...And you answered both my questions. Thanks a ton.

TheCanadian
June 4th, 2007, 01:02 AM
Glad to help :hr: