是否有任何简单(或建议)的方法可以将参数与自定义事件一起发送?甚至只是一种在两个类之间传递变量的方法?
我的程序是一种简单的 Civ/Age of Empires 游戏,您可以在其中将建筑物放置在瓷砖上。这将按如下方式工作:
- 玩家单击 HUD 上的图标,这会创建一个调度事件,该事件由 PLAYER 类接收。
- PLAYER 类根据持有(点击)的建筑物更改值。
- 玩家单击网格中的图块来放置它,这会调度一个由 PLAYER 类接收的事件。
- PLAYER 类创建一个建筑对象并将其添加到 PLAYER 类中的一个数组中。
我希望代码如何工作的一个例子:
图标.as
private function onMouseClick(e:MouseEvent = null):void {
var iconClickedEvent:Event = new Event("BUILDING_HELD", buildingType); // passes "buildingType" through the event somehow
stage.dispatchEvent(iconClickedEvent);
}
瓷砖.as
private function onMouseClick(e:MouseEvent = null):void {
var buildingPlacedEvent:Event = new Event("BUILDING_PLACED", xRef, yRef);// passes "xRef" & "yRef", the tile's co-ordinate
stage.dispatchEvent(buildingPlacedEvent);
}
播放器.as
private function init(e:Event):void {
stage.addEventListener("BUILDING_HELD", buildingHeld(buildingType));
stage.addEventListener("BUILDING_PLACED", placeBuilding(xRef, yRef));
}
private function buildingHeld(building:int):void {
buildingType = building;
}
private function placeBuilding(xRef:int, yRef:int):void {
switch(buildingType){
case 1: // main base
MainBaseArray.push();
MainBaseArray[length-1] = new MainBase(xPos, yPos); // create new object with the references passed
break;
}
}