I have a function, sceneLoader, that will instantiate an object of the Class Scene1 from a string using getDefinitionByName. I then want to access a function (sceneHandler) in Scene1, from another function.
public class Main extends MovieClip{
private var sceneNumber:int = 1;
private var SceneRef:Class;
private var sceneString:String;
public function Main(){
sceneLoader();
responseHandler();
}
private function sceneLoader():void{
sceneString = "Scene" + int(sceneNumber);
SceneRef = getDefinitionByName(sceneString) as Class;
var scene = new SceneRef();
addChild(scene);
}
private function responseHandler():void{
scene.sceneHandler(); //Obviously this will not work
}
}
Class Scene1
public class Scene1 extends MovieClip{
public function sceneHandler():void{
//Do something
}
}
The problem is that scene is out of scope. I cannot instantiate Scene1 outside the function as I need to first call getDefinitionByName. I also want to keep the function for loading scenes later on. How can I call sceneHandler from responseHandler?
EDIT: I've tried
public class Main extends MovieClip{
private var sceneNumber:int = 1;
private var SceneRef:Class;
private var sceneString:String;
public function Main(){
sceneLoader();
responseHandler();
}
private function sceneLoader():void{
addChild(getScene());//Does not work
}
private function responseHandler():void{
getScene().sceneHandler(); //Works now
}
private function getScene():Object{
sceneString = "Scene" + int(sceneNumber);
SceneRef = getDefinitionByName(sceneString) as Class;
var scene = new SceneRef();
//addChild(getScene());//This does work but doesn't suit my need
return scene;
}
}
Now I can't add the object to the stage. I get the Error:
Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.