ampo_webdesign
05-31-2007, 06:07 PM
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:
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:
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:
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?
I create a class definition in .as script, and save it in a file myClass.as:
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:
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:
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?