在ActionScript 3中动态加载类

Loading
classes dynamically in ActionScript 3 is easy, but there are a couple of tricks
to keep in mind.

ActionScript 3动态加载类很容易。有一些窍门便于使用。

First of all, to load a
class dynamically, use the
 flash.utils.getDefinitionByName function.
Be sure to pass in the fully qualified class name, like this:

首先,加载一个动态类可以使用flash.utils.getDefinitionByName
方法。确保使用的是完整的类名,例如:

var MyClass:Class =
getDefinitionByName("com.mydomain.package.MyClass") as Class;

var myInstance:MyClass = new MyClass();

 

Easy enough. But there’s
something to watch out for. Class definitions only get compiled into your SWF
if they’re declared somewhere in your code. In other words, if
 MyClass was never declared, the class definition wouldn’t be included in your SWF,
and you would get a runtime error when trying to load it dynamically. (Note
that this is not the case for classes native to the runtime — only your own
custom classes.) Even importing the class isn’t enough to get it included — it
must actually be declared.

很容易做到。但是有一些需要注意。如果他们使用在代码的某个地方,类的声明只编译到您的SWF中。换句话说,当它试图动态加载时,如果MyClass从未引用,类定义将不包括在您的SWF,你会得到一个运行时错误。 (请注意,这不是对原生类(即FLASH PLAYER自带的)情况只有你自己的自定义类。)即使引入类是不够的,得将它包含进去它实际上必须调用。

There
are two ways to work around this issue:

有两种方法解决这个问题:

1.          
Declare the class somewhere
in the code
.
在代码中的某个位置声明这个类

2.          
Use a command line
argument to force the class definition to be included.
Below
is an example of declaring the class before loading it dynamically:
使用命令行参数来强制类定义应包括在内。

下面是一个动态加载它之前宣布的类的例子:

MyClass;

// or

var mc:MyClass;

var MyClass:Class =
getDefinitionByName("com.mydomain.package.MyClass") as Class;

var myInstance:MyClass = new MyClass();

This
works fine, but in my opinion, it may defeat the purpose of loading a class
dynamically. Usually you want to load classes dynamically because you’re not
sure until runtime which class you’re going to want. If you knew which classes
you wanted before you loaded them dynamically, you could just instantiate them
directly.

The other option is to use
the mxmlc
 -includes compiler argument like
this:

这个工作得很好,但在我看来,它可以达到动态加载一个类的目的。通常你要动态加载的类,因为不是在你当前的代码中,直到运行时你会想知道。如果你想知道哪些类,然后再加载它们的动态,你可以只直接实例。
另一种选择是使用mxmlc –includes 编译器这样:

-includes=com.mydomain.package.MyClass

 

Now,
the class definition will be compiled into your SWF, and you can load it
dynamically anytime you want without ever having to declare it.

现在定义的类被编译到你的SWF中,你可以在任何没有声明他的情况下,随时动态加载它。

To
specify compiler arguments in Flash Builder, follow these steps:

具体在Flash Builder中的编译参数,遵循下列步骤:

1.          
Right-click on your
project.
右键你的项目

2.          
Choose “Properties”.选择“属性”

3.          
Select “Flex
Compiler”
.
选择“Flex 编译”

4.          
Enter your arguments
in the “Additional compiler arguments” section.
输入你的参数在“Additional compiler arguments”选项中

As an aside, you can use
the
 flash.utils.getQualifiedClassName andflash.utils.getQualifiedSuperclassName functions
to get the fully qualified class name of an object instance.

同时你可以使用

flash.utils.getQualifiedClassName flash.utils.getQualifiedSuperclassName
方法获得完整的类名和对象实例。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: