如何在AS3中运行Main()的另一个实例

时间:2013-02-20 15:06:51

标签: ios actionscript-3 air instance main

如何在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。

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

2 个答案:

答案 0 :(得分:0)

实际上你必须创建一个特殊的类容器并在主类中完全交换(创建新的或删除)。这个游戏类可以dispatch events,至少sprite可以发送事件。创建自定义事件并将它们传递到更高和更高级别将允许您在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)) ;
    }
  }
}

答案 1 :(得分: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"); 
    }

}

}

RunMain:

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