如何在 as3 中使用另一个 Class 的 Main
How to use Main from another Class in as3
有人可以帮我调用 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);
}
}
通过使用一个按钮,我删除了那个精灵,我的场景变成了空白,但我还有另一个 class:
public class AnotherClass
{
public function Action()
{
ResetMain();
}
private function ResetMain()
{
//what to write here for reseting Main(re-calling Main()) ?
}
}
我想,如果你真的想要,你可以删除文档 class 并重新实例化它:
//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
}
虽然这是一种重置程序的奇怪方法,但我不推荐它。执行此操作时,您将不再使用 root
关键字。任何未被弱引用或显式删除的事件侦听器也会导致内存泄漏。
最好只写一个重置所有东西的函数,而不是通过重新实例化文档来重新调用 Main 构造函数 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);
}
从您的评论来看,您似乎只需要一种方法来从 AnotherClass
引用您的主要 class。
有几种方法可以完成此操作。
创建 AnotherClass 时传递对它的引用。您没有展示如何创建 AnotherClass,但您可以更改它的构造函数以获取 Main 实例的引用:
public class AnotherClass
{
private var main:Main;
public function AnotherClass(main_:Main){
this.main = main_;
}
//....rest of class code
然后在实例化AnotherClass时,传入引用:
var another:AnotherClass = new AnotherClass(this); //assuming your in the main class with this line of code
使用root
关键字。
root 关键字为您提供了对 documentClass 的引用(我假设 Main 是)。因此,对于显示列表中的任何 class,您可以执行以下操作:
Main(root).reset();
静态引用 Main 实例
静态方法和变量是使用 class 本身访问的(不是 class 的实例)。所以你可以这样做:
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
然后您可以在应用程序的任何位置执行此操作:
Main.me.reset();
有人可以帮我调用 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);
}
}
通过使用一个按钮,我删除了那个精灵,我的场景变成了空白,但我还有另一个 class:
public class AnotherClass
{
public function Action()
{
ResetMain();
}
private function ResetMain()
{
//what to write here for reseting Main(re-calling Main()) ?
}
}
我想,如果你真的想要,你可以删除文档 class 并重新实例化它:
//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
}
虽然这是一种重置程序的奇怪方法,但我不推荐它。执行此操作时,您将不再使用 root
关键字。任何未被弱引用或显式删除的事件侦听器也会导致内存泄漏。
最好只写一个重置所有东西的函数,而不是通过重新实例化文档来重新调用 Main 构造函数 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);
}
从您的评论来看,您似乎只需要一种方法来从 AnotherClass
引用您的主要 class。
有几种方法可以完成此操作。
创建 AnotherClass 时传递对它的引用。您没有展示如何创建 AnotherClass,但您可以更改它的构造函数以获取 Main 实例的引用:
public class AnotherClass { private var main:Main; public function AnotherClass(main_:Main){ this.main = main_; } //....rest of class code
然后在实例化AnotherClass时,传入引用:
var another:AnotherClass = new AnotherClass(this); //assuming your in the main class with this line of code
使用
root
关键字。root 关键字为您提供了对 documentClass 的引用(我假设 Main 是)。因此,对于显示列表中的任何 class,您可以执行以下操作:
Main(root).reset();
静态引用 Main 实例
静态方法和变量是使用 class 本身访问的(不是 class 的实例)。所以你可以这样做:
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
然后您可以在应用程序的任何位置执行此操作:
Main.me.reset();