简体   繁体   English

在AS3中按类名称获取实例

[英]Get instances by class name in AS3

I need to get all the instances in my stage according to an especific class name. 我需要根据一个特定的类名来获取阶段中的所有实例。 I'm doing this: 我正在这样做:

var class_ref:Class = getDefinitionByName('fran.MyOwnClass') as Class;
var element;

for (var i:uint = 0; i < this.parent.numChildren; i++)
{
    element = this.parent.getChildAt(i);
    if (element is class_ref)
    {
        trace('Found element of class fran.MyOwnClass');
    }
}

But I want a better way (more efficiently, without checking all the MCs). 但是,我希望有一种更好的方法(更高效,无需检查所有MC)。 Is it possible? 可能吗?

If you can start tracking instances from the very beginning of you application life, I'd recommend simply add event listener: 如果您可以从应用程序生命的一开始就开始跟踪实例,我建议您只需添加事件监听器即可:

// in document class constructor, before doing anything else
stage.addEventListener(Event.ADDED, stage_addedHandler);
stage.addEventListener(Event.REMOVED, stage_removedHandler);

private function stage_addedHandler(event:Event):void
{
    var obj:DisplayObject = event.target as DisplayObject;
    // do something, e.g. if (obj is MyClass) objCounter++;
}
...

If you can't track from the beginning, you can't avoid loops.. Just make them more optimized: 如果您不能从一开始就进行跟踪,就无法避免循环。.只需对其进行优化即可:

var n:int = container.numChildren;
while (n-- > 0)
{
    ...
}

Overriding everywhere addChild() and others — that's simply impossible solution in real projects. 覆盖各处的addChild()和其他对象-在实际项目中这根本不可能解决。

You could keep a list of all the MC's of a certain type by extending the container class and overriding its addChild() , addChildAt() , removeChild() and removeChildAt() functions. 通过扩展容器类并覆盖其addChild()addChildAt()removeChild()removeChildAt()函数,可以保留特定类型的所有MC的列表。

public class MySprite extends Sprite {

    public var ownClasses:Vector.<MyOwnClass> = new Vector.<MyOwnClass>();

    override public function addChild(child:DisplayObject):DisplayObject {
        addOwnClass(child as MyOwnClass);
        return super.addChild(child);
    }

    override public function addChildAt(child:DisplayObject, index:int):DisplayObject {
        addOwnClass(child as MyOwnClass);
        return super.addChildAt(child, index);
    }

    private function addOwnClass(child:MyOwnClass):void {
        if (child) ownClasses.push(child);
    }

    override public function removeChild(child:DisplayObject):DisplayObject {
        removeOwnClass(child as MyOwnClass);
        return super.removeChild(child);
    }

    override public function removeChildAt(index:int):DisplayObject {
        removeOwnClass(getChildAt(index) as MyOwnClass);
        return super.removeChildAt(index);
    }

    private function removeOwnClass(child:MyOwnClass):void {
        if (child) {
            var i:int = ownClasses.indexOf(child);
            if (i != -1) ownClasses.splice(i, 1);
        }
    }

}

Using this class, every time a child is added, you check whether it's a MyOwnClass and if it is you add it to the ownClasses list. 使用此类,每次添加一个孩子时,您都要检查它是否是MyOwnClass ,如果是,则将其添加到ownClasses列表中。 Similar for removing children. 删除孩子的方法与此类似。

Now you can simply access the list when you need it without looping over the MC's. 现在,您可以在需要时简单地访问列表,而无需遍历MC。

public class Main extends MySprite
{
    public function Main()
    {
        addChild(new Sprite());
        addChild(new MyOwnClass());
        trace(ownClasses);
    }
}

This will output [object MyOwnClass] 这将输出[object MyOwnClass]

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

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