Solved

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

Posted on 2010-11-10
13
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Software Restriction Policy 8 897
Failed download 57 593
YouTube video issue 28 143
Adobe Products Forum? 3 173
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
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.

710 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