private function loadLevel(level : int, name : String) : void {
try {
var LevelClass : Class = getDefinitionByName("Level" + level) as Class;
// i think you should also create an ILevel interface for your levels
var level : ILevel = new LevelClass(name) as ILevel;
level.onInitialize.addOnce(onLevelReady);
level.loadData();
} catch(e:Error) {
trace("Failed: " + e.message)
}
}
private function somewhere() : void {
// The downside is that you need to make sure the actual Level1,Level2,... classes
// are included by mxmlc when compiling your swf. This can be done by refencing them somewhere in the code.
Level1;
loadLevel(1, "some Level");
}
another solution might look like this:
private function loadLevel(level : int, name : String) : void {
var level : ILevel;
switch(level) {
case 1:
level = new Level1(name);
break;
case 2:
level = new Level2(name);
break;
default:
throw new Error("No such Level");
}
level.onInitialize.addOnce(onLevelReady);
level.loadData();
}