这是我的自定义事件类:
package{
import flash.events.Event;
public class PetEvent extends Event{
public static const ON_CRASH:String = "onCrash";
public function PetEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false):void{
super(type, bubbles, cancelable);
}
override public function clone():Event {
return new PetEvent(type, bubbles, cancelable);
}
}
}
这是我的游戏处理程序。我创建了一个我想从中收听的 Surf 类的新实例。
package {
import flash.display.MovieClip;
import flash.events.Event;
public class GameHandler extends MovieClip {
public var newGame:Surf;
public function GameHandler() {
newGame = new Surf();
newGame.addEventListener(PetEvent.ON_CRASH, onCrash);
addChild(newGame);
}
public function onCrash(petEvent:PetEvent):void{
trace("MPAM");
var gameOver:GameOver = new GameOver(stage.stageWidth, stage.stageHeight);
addChild(gameOver);
newGame = null;
}
}
}
以及来自 Surf 类的相关行:
public function startSurfing(timerEvent:TimerEvent):void
{
moveCatandDog();
for each ( var boat:Boat in armada)
{
boat.moveBoat(boatSpeed);
if ( cat.hitTestObject(boat) || dog.hitTestObject(boat) )
{
dispatchEvent( new PetEvent(PetEvent.ON_CRASH) );
gameTimer.stop();
}
}
}
因此,当 Surf 检测到崩溃时,我希望它将事件发送到 GameHandler 并且 GameHandler 将创建一个 GameOver 实例。
我已经尝试了一切,我什至没有得到任何痕迹。我通常不问问题,但这是针对 uni 项目的,我的时间不多了。我真的很感激任何反馈。谢谢!