0

是否可以在 AS3 中创建“API”来添加影片剪辑/位图?我试过这样做:

public function addImage(name:String, x:int, y:int, type:String = "MovieClip", path:String = null) {
    var mc:MovieClip;

    if (type == "MovieClip") {
        var $name = "as_" + name + "()"
        mc = new $name;
        trace(mc);
    }
    else if (type == "Bitmap") {

    }
}

但它错误:

TypeError: Error #1007: Instantiation attempted on a non constructor

有这样做的正确方法吗?还是不能以这种方式将影片剪辑添加到舞台?

4

1 回答 1

3

$name是 a 的一个实例String。实例化对象所需的是对 a 的引用Class,您可以通过将类名传递给flash.utils.getDefinitionByName.

尝试这个:

var className:String = "as_" + name;
var MyClass:Class = getDefinitionByName(className) as Class;
mc = new MyClass();
于 2013-02-16T13:27:50.857 回答