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:
var myClass:Class = getDefinitionByName("Box") as Class;
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
import mx.containers.Box;
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:
private var _dummyVarToAddBoxToAppCompilation:Box;
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.
Tags: error, runtime errors
This post was written by Andrei Ionescu
Views: 5121










Or directly force the compiler to include this class with :
For example:
Hey, I ran in this problem, too. But I don’t like the solution with the variable. For example I want to load a configuration xml File for my application. This xml file contains a class string. I would like to use this string to create a Class variable. But it is impossible because of the problem that you described above… I understand this problem but it really sucks. Because it is a big limit for dynamic class instancing…
Do you know a workaround for this? THX
Mem, thanks for your comment.
Flo, I’m sorry that you’re in this nasty problem. I can understand why the linker and compiler work like this. The resulting application is targeted for web which limits our size, so the compiler excludes anything that is not directly used. Also by this way unused code lines from the Flex SDK will not be included either.
What I would if I were you… I would make a list with all possible classes that may be instantiated and add them to the whole application. Then using that XML config file you’ll use only what you need.
Doing this you’ll have control of what is included in your compiled application thus having a known size of your resulting swf file and you’ll also be able to use your external settings file.
I know that is not a fix for your problem but I hope it helps a bit.
@Andrei Ionescu Thanks a lot for your answer. Yeah it is not really good solution, but it helps
If i do not have many classes, that it is fine… otherwise I should to use Runtime Shared Librarys in which my classes are defined. That has the advantages that the filesize is not inscreasing a lot….
Hi, I ran into this same problem when building serialization between java and as3 classes. Both java and as3 classes are build by generating them from an uml document and then as3 classes are compiled to swc with compc that comes with flex sdk. When i compile the application in flash cs4, everything works fine, if there is a variable representing the class in the code. Problem is that there might soon be hundreds of classes that I need to be able to move between java server application and flash applet. What should I do?
Flo, you mentioned runtime shared libraries.. How to use them and in what way they could help? Sorry, if the question sounds stupid, but I’ve not used actionscript a lot yet, I’m mainly java developer and I dont know yet everything about this environment..
But if adding a variable to the class where getDefinitionByName is being used, I’m ready to ditch the whole idea and burn all flash licences at our company.
Hi,
Just built the serialization and figured out that my conserns were unnecessary after all. Because if we have a case where Component A waits for data that is of type B, and when component A receives message type B, it is of course put in a variable of type B inside component A. So class B is included in the compilation (because it is included with A) and unnecessary classes are not. I quess that it makes always sense after all (Well of course it does!) that class is never compiled if it is not used in code.
This makes sense. If the class is not included in the compilation, why in the world would I need the class’s definition. If it were the case, then an erroneus message must have been received by the component.
So I’ve been annoyed with this issue b/c I’m also creating a dynamic system that will be controlled by a config XML. It is said that you can just declare dummy vars for all possible views, which works and I can live with, but by declaring them all, am I adding to the swf file size? or do I need to instanciate for it to actually add to the file size?
Also, I’m able to do this approach just fine in Flash.
Hello Brad! You can do one of the following:
or
First example creates some variables and can be put in a class outside any method/function, second example calls the class but needs to be in method, function or in a place where ActionScript code is executed.
Only one of them is necessary. Both methods adds to the file size.
Hope this is clearing any confusion.
It seems to work when you pass in the whole path of the class.