简体   繁体   中英

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? Is there a way? Or I have to initialize every class I want to use?! That would be soooo stupid of Adobe!

getDefinitionByName() will work with any class name, linked to library or not. Your problem is likely that since you're not mentioning the class name in the code, it's not in your swf at all. 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.

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)

The long answer:

There is no other way to create a Class by its name than to use getDefinitionByName().

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.

If you use libraries there are three ways they can be referenced from your main application:

  • merged into code
  • runtime shared library (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.

The second option will compile the entire library into a separate swf, which is loaded by the main application. This results in a bigger file size, but the same swf can be used by several applications. 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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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