Solved

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

Posted on 2010-11-10
13
673 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Author Comment

by:rascalpants
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:section25
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now