Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-07-2008, 04:14 AM   #1
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 2,371
Send a message via AIM to Mazoonist
Default getDefinitionByName

Hey all, I'm trying to use getDefinitionByName to dynamically specify the name of a class. The code is on the timeline of a fla file, and the class in question is in the same folder as the fla. Here's my code:
ActionScript Code:
var tile:BasicTile; var tileClass:Class = getDefinitionByName("Tile0") as Class; tile = new tileClass();
I get this error:
Quote:
ReferenceError: Error #1065: Variable Tile0 is not defined.
By the way, typing tile to BasicTile is not a mistake, as Tile0 inherits from BasicTile. And I can directly instantiate an instance of Tile0, so the class is there. Must be there's something about getDefinitionByName that I don't know. I don't see much difference between my code and this code from the help files:
ActionScript Code:
package {     import flash.display.DisplayObject;     import flash.display.Sprite;     import flash.utils.getDefinitionByName;     public class GetDefinitionByNameExample extends Sprite {         private var bgColor:uint = 0xFFCC00;         private var size:uint = 80;         public function GetDefinitionByNameExample() {             var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;             var instance:Object = new ClassReference();             instance.graphics.beginFill(bgColor);             instance.graphics.drawRect(0, 0, size, size);             instance.graphics.endFill();             addChild(DisplayObject(instance));         }     } }
What am I doing wrong or not seeing?
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 11-07-2008, 04:37 AM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 2,371
Send a message via AIM to Mazoonist
Default

Edit: Okay, I thought I had it solved. But I keep getting this error even though I've moved the class to a package. If anyone has any insight, I'd be glad to know what it is. Argggh!

Edit: If anyone's interested, I found the answer by googling. The answer is on this page (among others, but this one says it well): http://nwebb.co.uk/blog/?p=186
__________________
My Tutorials * My Website

Last edited by Mazoonist; 11-07-2008 at 05:16 AM.
Mazoonist is offline   Reply With Quote
Old 11-07-2008, 08:54 AM   #3
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 907
Default

That's the reason why it's always a good idea to have a static NAME const in the classes you know you're going to create dynamic instances of. It automatically solves 2 problems:
1. You can be sure that a typo won't mess up your instantiation
2. No need to reference the class separately.

ActionScript Code:
//tile0.as public static const NAME : String = "Tile0"; //needs to match the classname exactly
ActionScript Code:
//instantiating class/timeline var tileClass:Class = getDefinitionByName( Tile0.NAME ) as Class;
Of course, you'd never do it like this, because it'd be stupid not to instantiate it directly.

If I remember correctly I've always needed an interface as well, something like
ActionScript Code:
//IReflect function getClassName() : String
Which gets implemented in (for example) Tile0.as as
ActionScript Code:
//Tile0.as public function getClassName() : String{     return NAME; }

Last edited by creynders; 11-07-2008 at 08:56 AM.
creynders is offline   Reply With Quote
Old 11-07-2008, 01:11 PM   #4
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 2,371
Send a message via AIM to Mazoonist
Default

Hey Creynders,

So, are you saying that if each class has a static const NAME, that class will get compiled without having to create a dummy variable for it (like the solution outlined by the link I gave above)?

Quote:
Of course, you'd never do it like this, because it'd be stupid not to instantiate it directly.
I'm not sure what you mean by that.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 11-08-2008, 07:30 AM   #5
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 907
Default

No, not exactly. It depends.
At some point or the other a string value of the class name is passed and somewhere else there'll be an evaluation of that string value. Now, I can't invent a good example of when and how to use this (though I've used it in the past, but I can't remember what situation called for this approach) Anyway, if for instance the evaluation happens in a separate swf, normally you'd need to add a reference to the class in that swf. If however, you evaluate using the NAME constant then automatically that swf will have a reference to that class and then there's no need to create a separate reference to the class definition.

And I meant that I wrote this line
ActionScript Code:
var tileClass:Class = getDefinitionByName( Tile0.NAME ) as Class;
only for the purpose of this example since, obviously you'd never do this, but you'd instantiate it "normally", like this:
ActionScript Code:
var tileClass : Tile0 = new Tile0();
creynders is offline   Reply With Quote
Old 11-08-2008, 04:08 PM   #6
pj-co
Site Contributor
 
pj-co's Avatar
 
Join Date: Jun 2008
Location: Brooklyn
Posts: 360
Default

It's not stupid at all. In fact I always thought this little command was very cool.

It's useful for when you have lots of library items and want to manage their 'linkage' in one place


I've posted this before but this is my LibrarayManager class:

ActionScript Code:
package com.pj_co.managers {     import flash.display.MovieClip;     import flash.utils.getDefinitionByName;         /**     * Central list of library items attached in code     * @usage provides a runtime method for access dynamically loaded assets from the lib     * @author pj-co     * Free to use under the open source MIT license     */     public class LibraryManager     {                 public static const ORANGE_LARGE : String   = "LargeRollGraphicsOrange";         public static const GREEN_LARGE  : String   = "LargeRollGraphicsGreen";         public static const TEAL_LARGE   : String   = "LargeRollGraphicsTeal";         public static const PURPLE_LARGE : String   = "LargeRollGraphicsPurple";         public static const BLUE_LARGE   : String   = "LargeRollGraphicsBlue";                 public function LibraryManager()         {                     }                 public static function getAsset ( assetLinkageID : String ) : MovieClip         {             var asset : Class = Class ( getDefinitionByName ( assetLinkageID ) ) ;                         return new asset() as MovieClip;         }     }    }


Then usage is simply this:

ActionScript Code:
import com.pj_co.managers.LibraryManager var mcFromTheLibrary : MovieClip = LibraryManager.getAsset( LibraryManager.ORANGE_LARGE );
pj-co is offline   Reply With Quote
Old 11-09-2008, 04:53 PM   #7
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 907
Default

The idea of having a LibraryManager is good, but I wouldn't use getDefinitionByName for it. You can just as easily use the class itself.
ActionScript Code:
package com.pj_co.managers {     import flash.display.MovieClip;     import flash.utils.getDefinitionByName;         /**     * Central list of library items attached in code     * @usage provides a runtime method for access dynamically loaded assets from the lib     * @author pj-co     * Free to use under the open source MIT license     */     public class LibraryManager     {                 public static const ORANGE_LARGE : Class   = LargeRollGraphicsOrange;         public static const GREEN_LARGE  : Class   = LargeRollGraphicsGreen;         public static const TEAL_LARGE   : Class   = LargeRollGraphicsTeal;         public static const PURPLE_LARGE : Class   = LargeRollGraphicsPurple;         public static const BLUE_LARGE   : Class   = LargeRollGraphicsBlue;                 public function LibraryManager()         {                     }                 public static function getAsset ( assetLinkageClass : Class ) : DisplayObject         {             return new assetLinkageClass() as DisplayObject;         }     }    }
You use it exactly the same, but it'll be a lot faster, because getDefinitionByName is VERY slow.
creynders is offline   Reply With Quote
Old 11-09-2008, 07:30 PM   #8
pj-co
Site Contributor
 
pj-co's Avatar
 
Join Date: Jun 2008
Location: Brooklyn
Posts: 360
Default

Quote:
Originally Posted by creynders View Post
The idea of having a LibraryManager is good, but I wouldn't use getDefinitionByName for it. You can just as easily use the class itself.

...

You use it exactly the same, but it'll be a lot faster, because getDefinitionByName is VERY slow.
Well I suppose the biggest advantage my method provides is that it allows for MCs that were loaded via a separate assets SWF. Though of course you need to be careful of application domain issues.

Thanks for the tip though I had really almost forgotten you could do it that way heh
pj-co is offline   Reply With Quote
Reply


Thread Tools
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing embedded BMP with getDefinitionByName? ryeguy ActionScript 3.0 4 10-26-2008 02:05 PM
getDefinitionByName() failing Chuck Taylor Flex 2, 3 & 4 1 09-13-2008 03:01 PM
getDefinitionByName reference error bubba ActionScript 3.0 0 10-22-2007 07:03 PM
getDefinitionByName superclass issue jamiehill ActionScript 3.0 4 10-09-2007 03:48 PM
Is getDefinitionByName efficient? kensuguro ActionScript 3.0 1 07-12-2007 03:04 PM


All times are GMT. The time now is 03:46 AM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.