To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 05-31-2007, 06:07 PM   #1
ampo_webdesign
Member
 
Join Date: May 2007
Location: Poland
Posts: 65
[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 06:36 PM.
ampo_webdesign is offline   Reply With Quote
Old 05-31-2007, 06:57 PM   #2
jjcorreia
Member
 
Join Date: Oct 2000
Posts: 93
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 is offline   Reply With Quote
Old 05-31-2007, 07:07 PM   #3
ampo_webdesign
Member
 
Join Date: May 2007
Location: Poland
Posts: 65
Jjcorreia, with that single innocent sweet line you helped mi a lot, thanks!
ampo_webdesign is offline   Reply With Quote
Old 05-31-2007, 07:39 PM   #4
jjcorreia
Member
 
Join Date: Oct 2000
Posts: 93
No problem. Anytime
jjcorreia is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:38 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.