从字符串中的ActionScript实例化一个类3 [英] Instantiate a class from a string in ActionScript 3

查看:205
本文介绍了从字符串中的ActionScript实例化一个类3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,在运行时,包含我想要实例化一个类的名称。我将如何做呢?

我读到建议使用 flash.utils.getDefinitionByName()

  VAR MyClass的:类= getDefinitionByName(package.className)为类;
VAR将myInstance:* =新MyClass的();
 

不过,这使我有以下错误:

  

[故障]异常,信息=的ReferenceError:错误#1065:变量的className没有定义

解决方案

我已经出来是简单地写出来的类名由分号在你的项目中分离出来,在任何地方,最简单的方法

例如。我在其中创建一个Assets.as文件这样的:

 包{

公共类资产{

    //要调用getDefinitionByName当从编译器避免错误
    //只列出所有未以其他方式在code引用的类:
    气球;
    云;
    FlyingHorse;
    FlyingPig;
    飞碟;
    齐柏林;
}
}
 

全部code为例/本教程是在这里:<一href="http://producerism.com/blog/flashpunk-dame-and-lua-tutorial-part-6/">http://producerism.com/blog/flashpunk-dame-and-lua-tutorial-part-6/

I've got a string which, in run-time, contains the name of a class that I want to instantiate. How would I do that?

I read suggestions to use flash.utils.getDefinitionByName():

var myClass:Class = getDefinitionByName("package.className") as Class;
var myInstance:* = new myClass();

However, that gives me the following error:

[Fault] exception, information=ReferenceError: Error #1065: Variable className is not defined.

解决方案

The easiest method I've come up with is to simply write the classnames out, separated by semicolons, anywhere in your project.

e.g. I create an Assets.as file with this in it:

package {   

public class Assets {       

    // To avoid errors from the compiler when calling getDefinitionByName
    // just list all of the classes that are not otherwise referenced in code:
    Balloon;
    Cloud;
    FlyingHorse;
    FlyingPig;
    UFO;
    Zeppelin;       
}
}

Full code example/tutorial on this is here: http://producerism.com/blog/flashpunk-dame-and-lua-tutorial-part-6/

这篇关于从字符串中的ActionScript实例化一个类3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆