PDA

View Full Version : Gathering all objects on stage



gravity223
November 19th, 2009, 05:42 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.

micken
November 19th, 2009, 05:50 PM
The same way you'd loop through anything else with children.


for (var i:Number = 0; i < stage.numChildren; i++)
{
var child:* = stage.getChildAt(i);
var properties:Properties = new Properties(child.x, child.y, child.scaleX, child.scaleY);
myPropertyArray.push(properties);
}


where Properties is some kind of object to hold the information you require.

gravity223
November 19th, 2009, 05:54 PM
Hi,

According to flash, there isn't such thing as :Properties.

EDIT: Saw your edit, will try now.

gravity223
November 19th, 2009, 06:02 PM
Hi,

I'm still a bit confused about the whole properties thing. How would I go about making it be able to hold my information? I tried something but got "1136: Incorrect number of arguments. Expected 0."

Thanks.

BeerOclock
November 19th, 2009, 06:11 PM
Nevermind the properties stuff. Just add each object to an array, and the 'properties' come with it:



var arr:Array = new Array();

for (var i:Number = 0; i < stage.numChildren; i++) {
arr.push(stage.getChildAt(i));
}

// if you want the scaleX for example, then
for (var i:Number = 0; i < arr.length; i++) {
trace(DisplayObject(arr[i]).scaleX);
}

micken
November 19th, 2009, 06:12 PM
Actionscript 3.0 adopts a great many object oriented programming concepts that you should read up on if you plan to go any further with your project.



public class Properties
{
public var x:Number = 0;
public var y:Number = 0;
public var scaleX:Number = 0;
public var scaleY:Number = 0;
public function Properties(_x:Number, _y:Number, _scaleX:Number, _scaleY:Number):void
{
x = _x;
y = _y;
scaleX = _scaleX;
scaleY = _scaleY;
}
}


That is a class (or Object if you will) that contains the data you're trying to keep track of.

gravity223
November 19th, 2009, 06:20 PM
Thanks both of you for the fast replies. I will be sure to look into OOP.

However, when I tried to do this:

var nametest = stage.getChildAt(0);
trace(namtest.name);

I got back "root1" and stage.getChildAt(1) says its out of bounds. So I tried taking away "stage." and that worked. So now I can get properties via "getChildAt(i)" and by making a var and using ".x" ".y" etc however, it brings back the default properties of the child itself, and not the one that has been manipulated on stage. How would I do this?

Thanks.