rascalpants
asked on
My For In loop does not work... why?
I am very perplexed by this... I have a for..in loop that does not work, but the exact equivalent of a normal for loop does work.
The code is below...
// below loop does not even trace the key!!
for ( var key:String in _questions_mc )
{
trace( "key " + key );
if ( key.indexOf("question") > -1 )
{
var clip:MovieClip = _questions_mc[ key ];
trace( clip.name );
if ( clip != _question1_mc )
{
clip.x = _question1_mc.x;
clip.y = _question1_mc.y;
clip.visible = false;
clip.enabled = false;
}
clip = null;
}
}
// this loop works fine...
var len:int = _questions_mc.numChildren;
for ( var i:int = 0; i < len; i++ )
{
var clip:MovieClip = _questions_mc.getChildAt( i ) as MovieClip;
if ( clip != _question1_mc )
{
clip.x = _question1_mc.x;
clip.y = _question1_mc.y;
clip.visible = false;
clip.enabled = false;
}
clip = null;
}
And obviously there are children in my _questions_mc, because the value of "len" is 4... just like expected.
So the only difference is how I am looping thru the items in the parent movieclip _questions_mc.
Can anyone see what I am missing?
rp
What is the error you are getting? Have you tried using the "debug" option to see exactly which line it is stopping on?
ASKER
no error at all... both sets of code run without throwing an error...
hence the reason I am perplexed...
rp
hence the reason I am perplexed...
rp
You want "for each" in
The for-in loop iterates through the properties of the object, which, for an array would be things like length.
The for-in loop iterates through the properties of the object, which, for an array would be things like length.
ASKER
it is not an array... it is an object (MovieClip)
_questions_mc is a MovieClip object with 4 children MovieClips in them.
the purpose of the for..in loop, in my case was to just look thru the children... if they have "question" in their instance name, then run the evaluation and set the properties of the individual question movieclips.
I am convinced it is a system bug(not ActionScript bug), but I have faith that someone will prove me wrong :)
anyone?
rp
_questions_mc is a MovieClip object with 4 children MovieClips in them.
the purpose of the for..in loop, in my case was to just look thru the children... if they have "question" in their instance name, then run the evaluation and set the properties of the individual question movieclips.
I am convinced it is a system bug(not ActionScript bug), but I have faith that someone will prove me wrong :)
anyone?
rp
The first loop will not work as in flash you cannot get an objects child elements which are dynamically added.
This is what seems to be happenning here.
Hence the later worked.
Warm REgards
Deepanjan Das
http://deepanjandas.wordpress.com
This is what seems to be happenning here.
Hence the later worked.
Warm REgards
Deepanjan Das
http://deepanjandas.wordpress.com
ASKER
can you explain that a bit more... possibly point me towards some reference materials...
I want to make sure I understand what you are saying...
Thanks
rp
I want to make sure I understand what you are saying...
Thanks
rp
To iterate through the children of a movie clip you need to do this:
This will effectively remove all of the children of a movie clip, but you can modify it to do whatever you want to the children clips.
//------------------------ ---------
var s:DisplayObject;
while (myclip.numChildren > 1) {
s=myclip.getChildAt(1);
myclip.removeChild(s);
s = null;
}
//------------------------ ---------- --
This will effectively remove all of the children of a movie clip, but you can modify it to do whatever you want to the children clips.
//------------------------
var s:DisplayObject;
while (myclip.numChildren > 1) {
s=myclip.getChildAt(1);
myclip.removeChild(s);
s = null;
}
//------------------------
You can't use a "For - In" loop to iterate through a movie clip. A movie clip's objects are referred to as children and the only way to address the children is with the "child " methods.
getChildAt, getChildByName, getChildIndex, etc.
So to iterate through them you need to address each object as a child of the parent.
getChildAt, getChildByName, getChildIndex, etc.
So to iterate through them you need to address each object as a child of the parent.
In brief:
The MovieClip _questions_mc is a kind of dynamic object. And you are adding children dynamically means on runtime.
In ActionScript, you cannot access the properties or children of a dynamic object whose properties are added on runtime. Also for..in statement is used for generic objects and array.
But if you had an object with fixed properties, you can access those with this iterations.
As in your case, you will have to loop through the numChildren to get hold of its children using getChildAt as stated above.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2#for..in
Hope this helps.
Warm Regards
Deepanjan Das
http://deepanjandas.wordpress.com
The MovieClip _questions_mc is a kind of dynamic object. And you are adding children dynamically means on runtime.
In ActionScript, you cannot access the properties or children of a dynamic object whose properties are added on runtime. Also for..in statement is used for generic objects and array.
But if you had an object with fixed properties, you can access those with this iterations.
As in your case, you will have to loop through the numChildren to get hold of its children using getChildAt as stated above.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2#for..in
Hope this helps.
Warm Regards
Deepanjan Das
http://deepanjandas.wordpress.com
ASKER
Okay... first of all, I have to apologize for not giving ALL of the details... this content is in a ScrollPane... which I believe to be the cause of the problem. Why... I am not sure yet.
The for..in loop works perfectly when the parent object (_questions_mc) is not using the reference to the scrollpane.content. (ie. _questions_sp.content )
For..In works on ALL objects, and since a MovieClip is an object, you can easily loop through the properties of that object and gain access to anything you want... My example is actually not currently using any dynamically loaded content. It is just a simple parent movieclip, with 4 child clips. The parent is loaded into the scrollpane using the IDE... not thru code, which I don't believe has anything to do with the problem.
When I pull the parent clip (_questions_mc) out onto the stage, the for..in loop works fine. Iterates thru the child clips and positions them as intended.
@ section25 - there is nothing in my code or my question that asked to remove a clip... so your example really has nothing to do with the question. Also, this statement: "You can't use a "For - In" loop to iterate through a movie clip." is completely false. A movieclip is an object, and I have been iterating thru them for years. You can even add your own values to a MovieClip, because it uses a Dynamic Class... meaning you can add properties to it, all you want. You don't need to use the Objects methods to access and manipulate the MovieClips properties... dynamic or otherwise. This is the first occasion where I have not been able to do this, and I am thinking it has to do with being in a scrollpane... or that _questions_mc is a reference to the content property of the scrollpane.
@ Deepanjandas - This statement is also false... "In ActionScript, you cannot access the properties or children of a dynamic object whose properties are added on runtime." A movieclip is dynamic, and because of this, not in spite of it, you can access the properties and children of this object... I can very easily do this...
var clip:MovieClip = someMovieClipOnStage_mc;
clip.myOwnProperty = "FlashIsCool";
clip.myName = "rascalpants";
for( var key:String in clip ) trace( clip[ key ] );
the above code will trace out... "FlashIsCool" and "rascalpants"
Also.... I think you might have it backwards... "But if you had an object with fixed properties, you can access those with this iterations." I think this is also wrong, because if you run the below code, only the dynamically added properties will be iterated through... so the below code still only traces out "FlashIsCool" and "rascalpants".
import flash.display.MovieClip;
var clip:MovieClip = new MovieClip();
this.addChild( clip );
clip.x = 25;
clip.y = 50;
clip.myOwnProperty = "FlashIsCool";
clip.myName = "rascalpants";
for( var key:String in clip ) trace( clip[ key ] );
Anyway... I appreciate the help, and I apologize for not adding the scrollpane details... this will probably be the issue.
rp
The for..in loop works perfectly when the parent object (_questions_mc) is not using the reference to the scrollpane.content. (ie. _questions_sp.content )
For..In works on ALL objects, and since a MovieClip is an object, you can easily loop through the properties of that object and gain access to anything you want... My example is actually not currently using any dynamically loaded content. It is just a simple parent movieclip, with 4 child clips. The parent is loaded into the scrollpane using the IDE... not thru code, which I don't believe has anything to do with the problem.
When I pull the parent clip (_questions_mc) out onto the stage, the for..in loop works fine. Iterates thru the child clips and positions them as intended.
@ section25 - there is nothing in my code or my question that asked to remove a clip... so your example really has nothing to do with the question. Also, this statement: "You can't use a "For - In" loop to iterate through a movie clip." is completely false. A movieclip is an object, and I have been iterating thru them for years. You can even add your own values to a MovieClip, because it uses a Dynamic Class... meaning you can add properties to it, all you want. You don't need to use the Objects methods to access and manipulate the MovieClips properties... dynamic or otherwise. This is the first occasion where I have not been able to do this, and I am thinking it has to do with being in a scrollpane... or that _questions_mc is a reference to the content property of the scrollpane.
@ Deepanjandas - This statement is also false... "In ActionScript, you cannot access the properties or children of a dynamic object whose properties are added on runtime." A movieclip is dynamic, and because of this, not in spite of it, you can access the properties and children of this object... I can very easily do this...
var clip:MovieClip = someMovieClipOnStage_mc;
clip.myOwnProperty = "FlashIsCool";
clip.myName = "rascalpants";
for( var key:String in clip ) trace( clip[ key ] );
the above code will trace out... "FlashIsCool" and "rascalpants"
Also.... I think you might have it backwards... "But if you had an object with fixed properties, you can access those with this iterations." I think this is also wrong, because if you run the below code, only the dynamically added properties will be iterated through... so the below code still only traces out "FlashIsCool" and "rascalpants".
import flash.display.MovieClip;
var clip:MovieClip = new MovieClip();
this.addChild( clip );
clip.x = 25;
clip.y = 50;
clip.myOwnProperty = "FlashIsCool";
clip.myName = "rascalpants";
for( var key:String in clip ) trace( clip[ key ] );
Anyway... I appreciate the help, and I apologize for not adding the scrollpane details... this will probably be the issue.
rp
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Well, I will leave this up for a bit longer to see if anyone else has come across this and why this happens... maybe a scope issue or referencing issue... I don't know.
thanks
rp
thanks
rp
ASKER
While there was no real answer to this question presented, I believe that he most helpful information presented was from Deepanjan.
thanks everyone... guess I will have to find out why the scrollpane is causing issue another time.
rp
thanks everyone... guess I will have to find out why the scrollpane is causing issue another time.
rp