Reputation: 8334
I am having problems getting items that i placed in my designer. I work with external classes i link to my objects i place in my designer on the timeline. I always assumed they where placed in the "stage" attribute in the as3 class.
However when i do:
trace(stage.getChildByName("thing"));
the item that i called thing in my designer is not found. It actually returns null.
Does anyone know what attribute i need to target to get the items i already placed on my stage. It would make everything a lot easier then dynamicly adding all the items.
Upvotes: 0
Views: 435
Reputation: 2457
My object's parent
For an instance named s
and placed on your Stage
, or called by the code like in the following example, you just have to trace it's parent
to know what you should write before your dot. Your object's parent is before your dot:
var s:Square = new Square();
addChild(s);
trace(s.parent); // [object MainTimeline]
trace(s.parent.parent); // [object Stage]
It means that your object is placed on your MainTimeline
, which is itself placed on it's Stage
.
To refer to the Stage
object of a DisplayObject
, you just have to use it's stage
property:
trace('s Stage ' + s.stage); // s Stage [object Stage]
trace('this Stage ' + this.stage); // this Stage [object Stage]
By using the keyword this
, you refer to your MainTimeline:
trace(this is MainTimeline); // true
Then to call your object you can write:
trace(s); // [object Square]
// or
trace(this.s); // [object Square]
Should I use getChildByName method?
You don't need to use getChildByName
method to call an object. You can directly call it by it's name. Why using a method when it's not necessary?
var s1:Square = new Square();
trace(s1); // [object Square]
this.addChild(s1);
s1.name = 'Square1';
trace(this.getChildByName("Square1")); // [object Square]
The difference is that getChildByName
returns null
if your object hasn't been added to the Display List by addChild
method.
But if you MUST ABSOLUTLY call instances of DisplayObjects by a method, call them by their indice with getChildAt
method, rather than by their name with getChildByName
method.
trace(this.getChildAt(0)); // [object Square]
You shouldn’t use getChildByName
method which is slower than getChildAt
method. You can compare the speed of these two methods with the following code:
// testing getChildByName
var sta:Number = getTimer();
for (var i:int = 0; i < 5000000; i++) {
getChildByName('Square1');
}
var sto:Number = getTimer();
trace((sto - sta) + ' ms'); // 2299 ms
// testing getChildAt
var sta:Number = getTimer();
for (var i:int = 0; i < 5000000; i++) {
getChildAt(0);
}
var sto:Number = getTimer();
trace((sto - sta) + ' ms'); // 610 ms
Upvotes: 1
Reputation: 60527
If memory serves, the objects place on the stage in the editor are a child of root
, not stage
.
If your code is on the timeline or the document class, the following should work.
this.getChildByName("thing");
If you are trying to access it from another object, the following should work.
(root as DisplayObjectContainer).getChildByName("thing");
Upvotes: 1