Hello everyone,
Im wondering if anyone can help me out with a little problem im having.
Ive been working on a small game and noticed i was re-using the same code several times so i decided to see if i can combine the code.
Heres my old code, which worked:
public function show_loader() {
loader_screen = new loader(this);
loader_screen.init_loader();
loader_screen.addEventListener( CustomEvent.PARAM_TYPE, custom_handler );
addChild(loader_screen);
}
public function hide_loader() {
if (loader_screen) {
removeChild(loader_screen);
loader_screen.removeEventListener( CustomEvent.PARAM_TYPE, custom_handler );
loader_screen = null;
}
return;
}
public function show_mainmenu() {
mainmenu_screen = new mainmenu(this);
mainmenu_screen.init_mainmenu();
mainmenu_screen.addEventListener( CustomEvent.PARAM_TYPE, custom_handler );
addChild(mainmenu_screen);
}
public function hide_mainmenu() {
if (mainmenu_screen) {
removeChild(mainmenu_screen);
mainmenu_screen.removeEventListener( CustomEvent.PARAM_TYPE, custom_handler );
mainmenu_screen = null;
}
return;
}
Thats only 2, i use it more often, basically to show and clear all the stages of the game.
So when i narrowed it down to a single function for all screens this is what i got:
public function show_screen(ref:String) {
trace(ref);
var GetName:String = ref;
var ClassName:Class = getDefinitionByName(GetName) as Class;
this[ref+"_screen"] = new ClassName(this) as Class;
this[ref+"_screen"].init_loader();
this[ref+"_screen"].addEventListener( CustomEvent.PARAM_TYPE, custom_handler );
addChild(this[ref+"_screen"]);
}
The passed variable to the function is the name of the class. However i get an error:
ReferenceError: Error #1065: Variabele loader is not defined.
Ive tried googling the error but havent found anything yet that solves the problem.
If anyone can help me with this would be great. If you need more information i can post whatevers needed.
Thanks,
Turbo