0

您将如何在 AS3 中运行 Main 函数的新实例?基本上我希望程序从一开始就重新运行,一旦它到达程序的末尾。我想过最后从树上删除我所有的孩子,然后再次运行第一种方法,但这似乎没有必要而且很混乱。该类扩展了 Sprite。

编辑:尝试创建一个自定义事件并使用dispatchEvent在程序结束时触发它,但有一点问题。这是我下面的当前代码。

整个 RunMain.as:

package  
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class RunMain extends Sprite {

        public function RunMain():void {

            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function loop(event:Event){

            removeEventListener(Event.ENTER_FRAME, loop);
            dispatchEvent(new EndEvent(EndEvent.END_EVENT));
        }

    }
}

整个 EndEvent.as:

package  
{
    import flash.events.Event;

    public class EndEvent extends Event 
    {
        // Event types.
        public static const END_EVENT:String = "event1";

        public function EndEvent(type:String = EndEvent.END_EVENT) 
        {
            super(type);
        }

        override public function clone():Event {
            // Return a new instance of this event with the same parameters.
            return new EndEvent(type);
        }
    }
}

Main.as 的部分内容:

public function Main() {
    runMain = new RunMain();
    runMain.addEventListener(EndEvent.END_EVENT, onProgramFinish);
    addChild(runMain);

    init();
    otherMethods();
}

public function onProgramFinish() {
    removeChild(runMain);

    runMain = null;

    runMain = new RunMain() ;
}

在我的 Main 类中,我onProgramFinish();在程序末尾有一行。

目前,当我运行编译器时出现错误:
ArgumentError:错误 #1063:Main/onProgramFinish() 上的参数计数不匹配。
预期为 0,得到 1。

当我选择继续时,程序运行良好,但它不会在它结束时运行新实例。

4

2 回答 2

0

事实上,您必须创建一个特殊的类容器并在主类中准确地交换他(创建新的或删除)。这个游戏类可以dispatch events,至少精灵可以调度事件。创建您的自定义事件并将它们传递给越来越高的级别将允许您在 Main 类中执行操作。

class Main extends Sprite {
  public var game:Game ;
  public function Main():void {
    game = new Game(new Player(), "asd") ;
    game.addEventListener(GameEvent.GAME_OVER, onGameOver) ;
    addChild(game) ;
  }

  public function onGameOver(){
    removeChild(game) ;
    //also you can try removing heavy children of game
    game = null ; //Let garbage collector know about object without reference.

    game = new Game(new Player(), "example") ;
  }
}

public class Game extends Sprite {

  public var hero:Hero = new Hero() ;
  public function Game(player:Player, name:String):void {

    addEventListener(Event.ENTER_FRAME, loop) ;
  }

  private function loop(event:Event){
    if (hero.hp == 0){
      removeEventListener(Event.ENTER_FRAME, loop) ;
      dispatchEvent(new GameEvent(GameEvent.GAME_OVER)) ;
    }
  }
}
于 2013-02-20T15:18:44.123 回答
0

您的活动:

package {
    import flash.events.Event;

public class ProgramEvent extends Event 
{

    public static const FINISH:String = "Finished" ;

    public function ProgramEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) 
    { 
        super(type, bubbles, cancelable);

    } 

    public override function clone():Event 
    { 
        return new ProgramEvent(type, bubbles, cancelable);
    } 

    public override function toString():String 
    { 
        return formatToString("ProgramEvent", "type", "bubbles", "cancelable", "eventPhase"); 
    }

}

}

运行主:

package  
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class RunMain extends Sprite {

        public function RunMain():void {

            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function loop(event:Event){

            removeEventListener(Event.ENTER_FRAME, loop);
            dispatchEvent(new ProgramEvent(ProgramEvent.FINISH));
        }

    }
}

主要:

public function Main() {
    startProgram() ;

    init();
    otherMethods();
}

public function onProgramFinish(event:ProgramEvent) {
    runMain.removeEventListener(ProgramEvent.FINISH, onProgramFinish) ;
    removeChild(runMain);
    runMain = null;
    startProgram() ;
}

public function startProgram(){
  runMain = new RunMain() ;
  runMain.addEventListener(ProgramEvent.FINISH, onProgramFinish);
  addChild(runMain);
}
于 2013-02-20T17:34:07.423 回答