简体   繁体   English

As3 / Flash:如何从设计器阶段获取对象

[英]As3/Flash: how to get object from the designer stage

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. 我一直假定它们放在as3类的“ stage”属性中。

However when i do: 但是当我这样做时:

trace(stage.getChildByName("thing"));

the item that i called thing in my designer is not found. 找不到我在设计器中称为事物的项目。 It actually returns null. 它实际上返回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. 动态添加所有项将使一切变得容易得多。

If memory serves, the objects place on the stage in the editor are a child of root , not stage . 如果有内存,则编辑器中放置在舞台上的对象是root的子stage ,而不是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");

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. 对于一个名为s并放置在您的Stage上的实例,或通过以下示例中的代码调用的实例,您只需跟踪parent即可知道在点之前应写的内容。 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 . 这意味着您的对象放置在MainTimeline ,而MainTimeline本身也放置在Stage

To refer to the Stage object of a DisplayObject , you just have to use it's stage property: 要引用DisplayObjectStage对象,只需使用它的stage属性:

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: 通过使用关键字this ,可以引用您的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? 我应该使用getChildByName方法吗?

You don't need to use getChildByName method to call an object. 您不需要使用getChildByName方法来调用对象。 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. 区别在于,如果还没有通过addChild方法将您的对象添加到显示列表中,则getChildByName返回null

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. 但是,如果您必须通过方法绝对调用DisplayObject的实例,请使用getChildAt方法按其索引来调用它们,而不要使用getChildByName方法按其名称来调用它们。

trace(this.getChildAt(0)); // [object Square] 

You shouldn't use getChildByName method which is slower than getChildAt method. 你不应该使用getChildByName方法,它是慢getChildAt方法。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM