Actionscript 3 如何在as3中使用另一个类中的Main

Actionscript 3 如何在as3中使用另一个类中的Main,actionscript-3,flash,oop,actionscript,flashdevelop,Actionscript 3,Flash,Oop,Actionscript,Flashdevelop,有人能帮我打电话给主构造函数吗?一般来说,我的想法是重置场景 主要类别: public class Main extends Sprite { private var sprite:Sprite = new Sprite(); public function Main() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } p

有人能帮我打电话给主构造函数吗?一般来说,我的想法是重置场景

主要类别:

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);
    }
}
通过使用一个按钮,我移除了那个精灵,我的场景变成空白,但我有另一个类:

public class AnotherClass
{
    public function Action()
    {
        ResetMain();
    }

    private function ResetMain()
    {
        //what to write here for reseting Main(re-calling Main()) ?
    }
}

我想,如果您真的愿意,可以删除document类并重新实例化它:

//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
关键字。任何未被弱引用或显式删除的事件侦听器也会导致内存泄漏

最好只编写一个重置所有内容的函数,而不是通过重新实例化document类(Main类)来重新调用主构造函数


从您的评论中,听起来您只是需要一种从另一个类
引用主类的方法

有几种方法可以实现这一点

  • 创建其他类时,将引用传递给该类。您不会显示如何创建另一个类,但可以更改其构造函数以获取主实例的引用:

    public class AnotherClass
    {
        private var main:Main;
    
        public function AnotherClass(main_:Main){
            this.main = main_;
        }
    
        //....rest of class code
    
    然后在实例化另一个类时,传入引用:

    var another:AnotherClass = new AnotherClass(this); //assuming your in the main class with this line of code
    
  • 使用
    root
    关键字

    root关键字为您获得了对documentClass的引用(我假设Main是)。因此,从显示列表上的任何类中,可以执行以下操作:

    Main(root).reset();
    
  • 对主实例进行静态引用

    使用类本身(而不是类的实例)访问静态方法和变量。所以你可以这样做:

    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();
    

  • 我照你说的做,但是error accure:error#1009:无法访问null对象引用的属性或方法。在另一个类中,我创建了变量“private var main:main;”,并在Action func中调用reset()-“main.reset();”。我想这是因为main==null,但是如何调用reset()我不知道。。。在另一个class.as中,我将“this”(来自Main.as)转换为“DisplayObjectContainer”,用于从另一个class.as添加场景中的元素。请回答。所有这些都很好,但是你能帮我理解它是如何工作的吗。。。例如我有另一个类,有“public var main\u class:main”;我从main这样称呼它:“var something:AnotherClass=new AnotherClass();something.main\u class=this;”。所以它工作得很好,但只有一次。为什么?因为我使用函数“ClearAll”,从场景中删除所有子级/事件和变量。如果我将一个类中的所有变量都设置为“null”-无论我如何使用它(你的变量还是我的变量),所有变量都可以正常工作。这就好像我需要在函数内部使用“new”语句,而不是像现在这样在它们外部使用。
    Main.me.reset();