I got a factory, where Action Script follows an xml and builds DisplayObject hierarchy out of it. It is meant that script doesn’t know beforehand what elements it will encounter in xml and therefore doesn’t know what user-defined factory classes it will need.
I know that it is possible to do something like this:
var rect:*, className:String = "flash.geom.Rectangle";
if (ApplicationDomain.currentDomain.hasDefinition(className)) {
rect = new(getDefinitionByName(className));
}
And Rectangle instance will get instantiated. But as soon as I replace flash.geom.Rectangle with something user-defined, like my.factory.Block it stops working and if I comment out conditional I get simple: “Variable Block is not defined” error.
Obvious workaround would be to create an instance of my.factory.Block (and all other components), prior to activating the factory, but that kinda ruins whole point of dynamic component factory.
Of course I have: import my.factory.*; statement at the top of the script.
Is there any smarter solution to this?
As others have pointed out, the problem is most probably that of classes not being included in the compiled swf. As Daniel pointed out, an import is not enough, you need a reference to the class. You don’t, however, need to declare a variable, as Cay stated, you can just do as follows:
MyClass;
Although, if you’re using the mxmlc to compile, you can use the -includes option to specify a class, or classes, that you would like to force to be included in the compiled swf. The nice thing about this is it doesn’t require you to have a reference in your code. It looks like this:
-includes com.example.MyClass com.example.MyOtherClass
If you have a really big package of classes you want to include, but don’t want to write out the class name for every class, you can compile the package as a swc, using the compc, and the use the -include-libraries option of mxmlc to include the entire package.