今天在用as编程时,遇到个问题
var ClassReference:Class = getDefinitionByName(chanelClassName) as Class;
这段代码无论如何也无法编译。
google了一下,就在网上找到了答案,顿时明白了这个原理;
英文贴子上有如下内容:
I’ve been playing with Tweensy these days. Tried to understand how it works and how to create effects using particles generator. At some point I run into an error like the following one:
ReferenceError: Error #1065: Variable Box is not defined.
at global/flash.utils::getDefinitionByName()
…
What triggered it is this part of code:
The error is saying that the it cannot retrieve the class code because the class is not compiled with the application. Even though you put something like
the class is not compiled with the application because the linker and the compiler do not add classes that are not referenced in the code.
What is the solution… Pretty simple and straight forward:
Even though you’ll never use the _dummyVarToAddBoxToAppCompilation variable it is necessary to use that line to instruct the compiler to include mx.containers.Box in the compilation.
You’ll find the same answer on multiple blogs and forums but still needed to make an article on it. More is always better and easier to find.
原理很答案,你的代码中没有一处直接用到这个getDefinitionByName(chanelClassName) as Class类
所以编译器不会把这个类编译到二进制code中,就造成了所谓的运行期错误。
如果要解决也很答案,只要为一个var 指定这个类就可以了。