|
-
[RESOLVED] getDefinitionByName - how does it actually work?
flash.utils.getDefinitionByName - how it is supposed to work?
I create a class definition in .as script, and save it in a file myClass.as:
PHP Code:
package {
import flash.display.Sprite;
public class myClass extends Sprite{
public function myClass(){
trace("I'm here, my name is myClass");
}
}
}
Then inside fla file (saved in the same folder with myClass.as) I try to fetch if with getDefinitionByName like that:
PHP Code:
import myClass;
import flash.utils.getDefinitionByName;
var klas:Class=getDefinitionByName("myClass") as Class;
I expect to get a class reference, but what i get instead is this error:
Code:
ReferenceError: Error #1065: Variable myClass is not defined.
at global/flash.utils::getDefinitionByName()
at test_fla::MainTimeline/test_fla::frame1()
When I tried to fetch a class reference from a library using the getDefinitionByName() method, it worked ok.
Could somebody explain what is the difference, why it doesn't fetch the classes defined in external as files and does fetch ones on Flash library? Is it possible to make the getDefinitionByName (or some other method) fetch a class from external AS file anyhow?
P.S. The deeper reason of that post is the following problem:
I want to create an instance of a class which name is a variable. So, let's say I have a navigation tool that allows to choose between page1, page2 and page3. The navigation tool returns just the number 1, 2 or 3. I have three classes: Page1, Page2 and Page3 and I want to create an instance object of one of those 3 classes, depending on the incoming navigation number. So when the number comes, I add it to the word "Page" and get the class name, ex. "Page2". Now I'd like to fetch the class definition of that name, to make an instance of it. For fetching the class definition I use the getDefinitionByName method. And as I said - if the classes Page1, Page2 and Page3 sit in the Flash library, this getDefinitionByName method works. But if create the Page1, Page2 and Page3 proper .as class definitions, the method getDefinitionByName fails and return the ReferenceError: Error #1065. Anybody knows why?
Last edited by ampo_webdesign; 05-31-2007 at 05:36 PM.
-
It because myClass is not being compiled into your file. Just because you import a class doesnt mean it gets compiled. Flash will check for any usage of that class and only compile it if it finds any. It does this so that you wont have extra code compiled that never gets used.
In this case, you can see the difference by adding a reference to the class so that the compiler includes it:
PHP Code:
var classReference:myClass;
So try this in your fla and it shouldnt throw an error:
PHP Code:
import myClass;
import flash.utils.getDefinitionByName;
var classReference:myClass;
var klas:Class=getDefinitionByName("myClass") as Class;
-
Jjcorreia, with that single innocent sweet line you helped mi a lot, thanks!
-
No problem. Anytime
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|