Ultrashock Forums > Flash > ActionScript
AS3 - using getDefinitionByName and initializing classes dynamically

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - using getDefinitionByName and initializing classes dynamically
Old 2010-02-09

Hi,

I wanna import and initialize my classes using getDefinitionByName just like this:

Code:
var ClassReference:Class = getDefinitionByName("com.doitflash.utils.scroll.ScrollBar") as Class;
var instance:* = new ClassReference();
But it gives an ReferenceError and says:
Code:
ReferenceError: Error #1065: Variable ScrollBar is not defined.
	at global/flash.utils::getDefinitionByName()
	at Untitled_fla::MainTimeline/frame1()
Please help, what's wrong?! it's not like that?
Please help

Regards,
Ali
postbit arrow 11 comments | 262 views postbit arrow Reply: with Quote   
Registered User
ponteha is offline
seperator
Posts: 49
2007-05-15
seperator

Ultrashock Member Comments:
JimTester's Avatar JimTester JimTester is offline JimTester lives in United Kingdom 2010-02-09 #2 Old  
I've never done this before but I found some code on Mike Chambers blog that might work for you:

Try:

var ClassReference:Class = getDefinitionByName("com.doitflash.utils.scroll.ScrollBar") as Class;

var instance:Scrollbar = (new ClassReference() as Scrollbar);

I think ClassReference repalces the constructor for Scrollbar so if your scrollbar is expecting any arguments in its constructor then try sticking them between the brackets of ClassReeference()...

Let me know if it works...
Reply With Quote  
ponteha ponteha is offline 2010-02-09 #3 Old  
Thanks JimTester, but it gives an Error, we can't say the type of "instance" variable is ScrollBar...

And apart from that, we want to initialize the class dynamically, it means that the String argue that getDefinitionByName is going to get is dynamic, ScrollBar class is just an example, if we knew the class name we didn't use getDefinitionByName simply
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2010-02-09 #4 Old  
You still need to import the class before you can call it like that. I have used that technique to build sites dynamically. I pass in the class name from the xml corresponding to the section. Before all of that I always force compile the classes (static class that just imports the classes).
Reply With Quote  
M2Media M2Media is offline M2Media lives in Netherlands 2010-02-10 #5 Old  
As Isocase says: the class still needs to be imported. Because you do not use the class, you just use a string to retrieve it, it will not be compiled.

You have two options: force the compilation by creating an instance of the class somewhere or use a factory method to retrieve an instance of the class:

Code:
// Create an instance but never use it
new com.doitflash.utils.scroll.ScrollBar();
var ClassReference:Class = getDefinitionByName("com.doitflash.utils.scroll.ScrollBar") as Class;
var instance:* = new ClassReference();
Code:
function getClass(name:String):Class {
var returnClass:Class;
switch(name) {
case "com.doitflash.utils.scroll.ScrollBar":
returnClass = new com.doitflash.utils.scroll.ScrollBar();
break;
}
return returnClass;
}
var instance:* = getClass("com.doitflash.utils.scroll.ScrollBar");
I prefer the second as in the first option it is not immediately clear why you are creating a new instance of com.doitflash.utils.scroll.ScrollBar.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2010-02-10 #6 Old  
Just a quick note: you don't need to create a new class instance in order to have it compiled:
Code:
package {
    import foo.*;
    public class MyClasses {
        FooApple;
        FooBanana;
        FooCherry;
    }
}
As long as you remember to reference the MyClasses class, all the Foo* classes will compiled.
Reply With Quote  
ponteha ponteha is offline 3 Weeks Ago #7 Old  
M2Media we can't initialize the class like this: returnClass = new com.doitflash.utils.scroll.ScrollBar();
it gives an error and says: Access of undefined property com.

I also want to initialize every classes! I want to initialize them dynamically... ScrollBar class is just an example, it can be any class that I don't know their names right now...

But getDefinitionByName doesn't work!!

Any other ideas please, I'm still confused
Thanks
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 3 Weeks Ago #8 Old  
You can't initialize a class if you don't know the name of it. In order to initialize a class you need a reference to the class, to get a reference to the class it needs to be compiled into a SWF file, and to compile the class into a SWF file you need to know the name of the class so it can be imported.

You could create a factory to initialize classes for you based on an ID or something, but one way or another those classes will need to be imported and compiled.
Reply With Quote  
ponteha ponteha is offline 3 Weeks Ago #9 Old  
Yep Nutrox, I have used this method before...
Factory method is like this for example: we make a switch method that has 3 cases, we pass each time a different String to initialize different classes each time, so it's not completely a great OOP programming! you know what I'm saying? because let's say in the Pizza example here http://en.wikipedia.org/wiki/Factory_method_pattern we have 3 cases, but if we decide to add an other kind of pizza we should add one more case in the switch function too...

But if we could run getDefinitionByName method, we didn't have to have some limited cases... we could every time give a new class address to initialize it... and if we give a wrong address, it gives an error that we ourself can handle that error too...

That is what I'm trying to do because I already tried factory method, but It's not perfect!
Reply With Quote  
ponteha ponteha is offline 3 Weeks Ago #10 Old  
flash example is this:

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));
        }
    }
}
it initialize the Sprite class but why we can't initialize our own classes like this?!
Please, what's the reason?
I have seen that my brother have done this one time, but I don't know how he could do it after seeing his code many times and when I ask him about it he doesn't know how he could do it himself?!
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 3 Weeks Ago #11 Old  
Quote: Originally Posted by :)) View Post
it initialize the Sprite class but why we can't initialize our own classes like this?!
Because you need to import them.

Code:
import com.doitflash.utils.scroll.ScrollBar;

var cls:Class = getDefinitionByName( "com.doitflash.utils.scroll.ScrollBar" ) as Class;
var bar:ScrollBar = new cls();
If that doesn't work then you haven't got your class paths setup correctly.
Reply With Quote  
ponteha ponteha is offline 3 Weeks Ago #12 Old  
Yep, it works... and it seems it's the only way
we should first import it.

But if we could do something it would be better, but it's just the way it is!
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: