简体   繁体   English

AS3 字符串到 class

[英]AS3 string to class

How can I convert a string to a class without getDefinitionByName because that class is not linked to a library item and getDefinitionByName only works with classes that are linked to library or allready initialized?如何在没有 getDefinitionByName 的情况下将字符串转换为 class 因为 class 未链接到库项目并且 getDefinitionByName 仅适用于链接到库或已初始化的类? Is there a way?有办法吗? Or I have to initialize every class I want to use?!或者我必须初始化我想使用的每个 class?! That would be soooo stupid of Adobe!这对 Adobe 来说太愚蠢了!

getDefinitionByName() will work with any class name, linked to library or not. getDefinitionByName()将与任何 class 名称一起使用,无论是否链接到库。 Your problem is likely that since you're not mentioning the class name in the code, it's not in your swf at all.您的问题很可能是因为您没有在代码中提及 class 名称,所以它根本不在您的 swf 中。 You will have to make sure the class is used at atleast one place in your code, if not it will not be compiled in.您必须确保 class 在您的代码中至少有一处使用,否则将不会被编译。

This can be as simple as just putting them on a line in your Main-class:这可以像将它们放在主类中的一行一样简单:

public class Main {

    public function Main(){
        ClassYouWant;
        AnotherClass;

        codeThatDoesStuff();
    }

}

The short answer:简短的回答:

If you want to avoid including class references (as suggested by @grapefrukt) you'll have to compile those classes in a library and reference that from your main application as an RSL (Runtime Shared Library)如果您想避免包含 class 引用(如@grapefrukt 建议的那样),您必须在库中编译这些类,并将主应用程序中的这些类作为 RSL(运行时共享库)引用

The long answer:长答案:

There is no other way to create a Class by its name than to use getDefinitionByName().除了使用 getDefinitionByName() 之外,没有其他方法可以通过其名称创建 Class。

However there are several ways to compile your code.但是,有几种方法可以编译您的代码。 By default, if you have a single application that doesn't include libraries, it will not compile classes that are not referenced in your code.默认情况下,如果您有一个不包含库的应用程序,它将不会编译代码中未引用的类。 You call this stupid, but there's a pretty good reason for that: it reduces the size of your swf and so the download time of your application.你称这很愚蠢,但这样做有一个很好的理由:它减少了 swf 的大小,从而减少了应用程序的下载时间。

If you use libraries there are three ways they can be referenced from your main application:如果您使用库,则可以通过三种方式从主应用程序中引用它们:

  • merged into code合并到代码中
  • runtime shared library (RSL)运行时共享库 (RSL)
  • external外部的

The first option will take all (and only) the classes from the library that were referenced in the main application.第一个选项将获取主应用程序中引用的库中的所有(且仅)类。 This results in a smaller swf size.这导致较小的 swf 大小。

The second option will compile the entire library into a separate swf, which is loaded by the main application.第二个选项会将整个库编译成一个单独的 swf,由主应用程序加载。 This results in a bigger file size, but the same swf can be used by several applications.这会导致更大的文件大小,但相同的 swf 可以被多个应用程序使用。 And what's most important to you: all classes are in that swf, so they can be referenced by getDefinitionByName() without having to include the class in your main app.对您来说最重要的是:所有类都在该 swf 中,因此它们可以被 getDefinitionByName() 引用,而不必在您的主应用程序中包含 class。

在此处输入图像描述

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

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