| Ultrashock Forums
• AS3 - using getDefinitionByName and initializing classes dynamically |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
2010-02-09
#2 |
||
|
|
|
2010-02-09
#3 |
||
|
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
|
|
|
2010-02-09
#4 |
||
|
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).
|
|
|
2010-02-10
#5 |
||
|
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");
|
|
17 Creative Assets
|
2010-02-10
#6 |
||
|
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;
}
}
|
|
|
|
3 Weeks Ago
#7 |
||
|
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 |
|
17 Creative Assets
|
3 Weeks Ago
#8 |
||
|
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. |
|
|
|
3 Weeks Ago
#9 |
||
|
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! |
|
|
|
3 Weeks Ago
#10 |
||
|
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));
}
}
}
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?! |
|
17 Creative Assets
|
3 Weeks Ago
#11 |
||
|
it initialize the Sprite class but why we can't initialize our own classes like this?!
Code:
import com.doitflash.utils.scroll.ScrollBar; var cls:Class = getDefinitionByName( "com.doitflash.utils.scroll.ScrollBar" ) as Class; var bar:ScrollBar = new cls(); |
|
|
|
3 Weeks Ago
#12 |
||
|
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! |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|


11 comments
| 262 views


17 Creative Assets

Linear Mode
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...