如何在AS3中运行Main函数的新实例?基本上,我希望程序从一开始就重新运行,一旦它到达程序的末尾。我想过最后把我所有的孩子都从树上移除,然后再次运行第一种方法,但这似乎没有必要,也很混乱。该类扩展了Sprite。
EDIT:试图创建一个自定义事件,并在程序结束时使用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);
}
}
}
主要部件:
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。
当我选择继续时,程序运行良好,但在结束时不会运行新实例。
事实上,您必须创建一个特殊的类容器,并在主类中完全交换(创建新容器或删除容器)。这个游戏类可以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)) ;
}
}
}
您的活动:
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);
}