Could someone help me with calling Main constructor? In general my idea is to reset scene.
Main class:
public class Main extends Sprite
{
private var sprite:Sprite = new Sprite();
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
sprite.graphics.lineStyle(1, 0x990000, 1);
sprite.graphics.drawRoundRect(5, 5, 500, 150, 10, 10);
addChild(sprite);
}
}
By using a button I removed that sprite, and my scene goes to blank, but i have another class:
public class AnotherClass
{
public function Action()
{
ResetMain();
}
private function ResetMain()
{
//what to write here for reseting Main(re-calling Main()) ?
}
}
I suppose, if you really wanted to, you could remove the document class and re-instantiate it:
//a function in your Main class that resets itself:
public function resetMain()
{
var s:Stage = stage; //need a temporary reference to the stage
parent.removeChild(this); //remove the Main class from the stage
s.addChild(new Main()); //add a new instance of the Main class
}
This is an odd way to reset your program though, and I wouldn't recommend it. You'll loose the use of the root
keyword doing this. Any event listeners not weakly referenced or explicitly removed would also cause memory leaks.
It would be better to just write a function that resets all your stuff, instead of re-calling the Main contructor by re-instantiating the document class (Main class)
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
reset();
}
public function reset():void {
//clear out any vars/display objects if they exist already
if(sprite) removeChild(sprite);
//now create those objects again
sprite = new Sprite();
sprite.graphics.lineStyle(1, 0x990000, 1);
sprite.graphics.drawRoundRect(5, 5, 500, 150, 10, 10);
addChild(sprite);
}
From your comments, it sounds like you just need a way to reference your main class from AnotherClass
.
There are a few ways you can accomplish this.
Pass a reference to AnotherClass when you create it. You don't show how you create AnotherClass, but you could change it's constructor to take in a reference of the Main instance:
public class AnotherClass
{
private var main:Main;
public function AnotherClass(main_:Main){
this.main = main_;
}
//....rest of class code
Then when you instantiate AnotherClass, pass in the reference:
var another:AnotherClass = new AnotherClass(this); //assuming your in the main class with this line of code
Use the root
keyword.
The root keyword gains you a reference to the documentClass (what I assume Main is). So from any class that is on the display list, you could do:
Main(root).reset();
Make a static reference to the Main instance
Static methods and variables are accessed using the class itself (not an instance of the class). So you could do something like this:
public class Main extends Sprite
{
public static var me:Main; //a static var to hold the instance of the main class
public function Main()
{
me = this; //assign the static var to the instance of Main
//.... rest of Main class code
Then you can do this from anywhere in the application:
Main.me.reset();