PDA

View Full Version : Gathering all objects on stage


danc
11-19-2009, 10:32 PM
Hi,

How would I go about looping through each object on the stage (that has been added through addChild()) and getting its properties (x, y, scaleX, scaleY) and adding it into an array?

Thanks.

RogerClark
11-20-2009, 07:02 AM
You can easily iterate through all the items on the display list using getChildAt(
See http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#getChildAt%28%29

But as far as I'm aware,unless you specifically add an extra attribute to items added programaticlly i.e. using addChild, there will be no way to tell them apart from item which Flash has added to the stage i.e. using the IDE.

It would be better just to save the items into an array when you call addChild in the first place

jonathanyuen
11-20-2009, 07:55 AM
Hi,

Hope this code snipplet functionality is what you're looking for.


var totalStageChildren : int = stage.numChildren;
var pop : DisplayObject;
var list : Array = [];

for (var i : int = 0; i < totalStageChildren; i++) {
pop = stage.getChildAt(i);
trace(pop.x, pop.y, pop.scaleX, pop.scaleY) /* do your thing */
list.push(pop);
}

trace(totalStageChildren, list.length); /* the numbers should tally */



RogerClark: Symbols added through Flash IDE will always exist as child inside the main document class object. So based on his question, I assume danc wants to count the display objects he added onto the stage programmatically via actionscript.

Hope this help. :)

dimpiax
11-20-2009, 08:07 AM
Not so smart function.
I will show example soon.
cya.

RogerClark
11-20-2009, 08:28 AM
Hi jonathanyuen,

I presumed when danc said that objects added to the "stage" using addChild that he actually ment the main document class i.e root.

I'm not sure how common it is to add objects directly to the stage rather than to the display list for the document class i.e. root

But if thats what he's doing the your code would would assuming you ignore the object root1 which is the display object of the document class

danc
11-20-2009, 09:32 AM
Hi,

Thanks both for you help. Because the objects I am itterating through get manipulated (startDrag, scale, rotate.. etc) on stage, getChildAt will get the properties of the default x, y, scaleX etc.. of the child itself, and not the one modified on stage.

RogerClark, you said I could add the items into an array when I call addChild in the first place. Since x, y, scaleX, scaleY get modified, I take it this would only put the original coordinates into the array and not the modified ones?

raghu_thejus
11-20-2009, 09:35 AM
u can set the default values at first itself like:


myMc.XX=myMc.x;
myMc.YY=myMc.y;
myMc.SCLX=myMc.scaleX;
myMc.SCLY=myMc.scaleY;


and at a later time u can acess the default value and set it


myMc.x=myMc.XX;

RogerClark
11-20-2009, 09:36 AM
Hi Danc,

Just store instances to each object you add (using addChild) into the array, then when you want to know their current positions just iterate through the objects in the array and retrieve their current x,y, etc.

i.e. when you do addChild(theDisplayObject) it doesn't make a copy of theDisplayObject it just puts a reference to it on the display list.

danc
11-20-2009, 09:47 AM
Hi RogerClark,

I see what you mean now. The problem is, I'm not entirely sure how to retrieve the modified x and y for a child. It always brings back the default ones set inside the child to tell it what size it should be by default, which is ScaleX of 1, scaleY of 1. It is spawned in the center of the stage, so it always brings back those center coordinates, even if I move the object around on stage.

I have a movieclip saved to the library with the name "mc_Square" with a class name of "dragsquare" and inside this movieclip is another movieclip with an instance name of "mcsquare". This contains all the code for dragging, rotating, default positions etc.. On the main timeline, frame 4, I do this:

var mcsquare:dragsquare = new dragsquare();
mcsquare.x = x;
mcsquare.y = y;
mcsquare.scaleX = 1;
mcsquare.scaleY = 1;
addChild(mcsquare);

This is all done on the main timeline. How would I go about retrieving the modified values of mcsquare once it has been spawned? This array method would be perfect, I just need to figure out how to get the constantly-updated properties for mcsquare once it gets spawned.

Thanks.

mark.knol
11-20-2009, 09:56 AM
You could use Sprite.transform.matrix to get its properties.

RogerClark
11-20-2009, 10:03 AM
Hi Danc,

I'm not sure about your code organisation but can't you just do this



var dispObjArr:Array=[];

var mcsquare:dragsquare = new dragsquare();
mcsquare.x = x;
mcsquare.y = y;
mcsquare.scaleX = 1;
mcsquare.scaleY = 1;
addChild(mcsquare);

dispObjArr.push(mcsquare);
//push all object you instantiate onto the array



Later do this to get current positions etc

for (var i:int=0;i<dispObjArr.length;i++)
{
trace(dispObjArr[i].x,dispObjArr[i].y );
}

danc
11-20-2009, 10:08 AM
Hi RogerClark,

I tried this just now, but it still brings back the default coordinates for the mcsquare, where it was spawned in the first place.

475 235