Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

My For In loop does not work...  why?

Posted on 2010-11-10
13
Medium Priority
?
685 Views
Last Modified: 2012-05-10

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;
				
}

Open in new window



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
0
Comment
Question by:rascalpants
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 8

Expert Comment

by:section25
ID: 34104349
What is the error you are getting? Have you tried using the "debug" option to see exactly which line it is stopping on?
0
 
LVL 22

Author Comment

by:rascalpants
ID: 34104421
no error at all...  both sets of code run without throwing an error...


hence the reason I am perplexed...


rp
0
 
LVL 11

Expert Comment

by:petiex
ID: 34104763
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Author Comment

by:rascalpants
ID: 34104831
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
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34108350
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
0
 
LVL 22

Author Comment

by:rascalpants
ID: 34108835
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
0
 
LVL 8

Expert Comment

by:section25
ID: 34108872
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;
}
//------------------------------------
0
 
LVL 8

Expert Comment

by:section25
ID: 34108893
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.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34109148
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
0
 
LVL 22

Author Comment

by:rascalpants
ID: 34112896
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


0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 1500 total points
ID: 34117536
Thanks rp for putting it up.
You are right, I actually just put the wrong words..
The for..in statement iterates only through the dynamic properties of an object, not the fixed properties. A fixed property is a property that is defined as part of a class definition.

Sorry Guys.

Warm Regards
Deepanjan Das
http://deepanjandas.wordpress.com 
0
 
LVL 22

Author Comment

by:rascalpants
ID: 34121800
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
0
 
LVL 22

Author Comment

by:rascalpants
ID: 34140114
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question