ActionScript 3.0-带参数的自定义事件 [英] Actionscript 3.0 - Custom Events with Parameters

查看:128
本文介绍了ActionScript 3.0-带参数的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法或建议的方法来将参数与自定义事件一起发送?甚至只是在两个类之间传递变量的一种方式?

Is there any simple -- or suggested -- way in order to send a parameter along with a custom event? Or even just a way to pass variables between two classes?

我的程序是一种简单的Civ / Age of Empires游戏,您可以在建筑物上放置建筑物。它将按以下方式工作:

My program is a sort of simple Civ/Age of Empires kind of game, where you can place buildings on tiles. This would work as follows:


  • 玩家单击HUD上的图标,这将创建一个调度事件,该事件由PLAYER类接收

  • PLAYER类根据所持建筑物(单击)更改值。

  • 玩家单击网格中的图块以放置

  • PLAYER类创建一个建筑对象并将其添加到PLAYER类内的数组中。

  • Player clicks on the icon on the HUD, which creates an dispatches an event which is received by the PLAYER class.
  • The PLAYER class changes a value depending on which building is held (clicked on).
  • Player clicks on the tile in the grid to place it, which dispatches an event which is received by the PLAYER class.
  • The PLAYER class creates a building object and adds it to an array within the PLAYER class.

我希望代码如何工作的示例:

An example of how I'd want the code to work:

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

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

player.as

player.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;
        }
}


推荐答案

最好的管理方法是为每个事件(或事件类型)创建自定义事件类。
如果创建一个继承 Event 的类,则该类将以与标准Event相同的方式使用,但可以包含自定义值或方法。

The best way to manage this is to create custom event classes for each of your events (or event types). If you create a class that inherit Event, it will be usable in the same ways that a standard Event, but can contain custom values or methods.

以下是此类的示例:

public class BuildingEvent extends Event {

  // contains an event name. Usefull to ensure at compile-time that there is no mistape in the event name.
  public static const BUILDING_HELD:String = "BUILDING_HELD";

  private var _buildingType:int;

  // the constructor, note the new parameter "buildingType". "bubbles" and "cancelable" are standard parameters for Event, so I kept them. 
  public function BuildingEvent(type:String, buildingType:int, bubbles:Boolean = false, cancelable:Boolean = false) {
    super(type, bubbles, cancelable);
    _buildingType = buildingType;
  }

  // using a getter ensure that a listening method cannot edit the value of buildingType.
  public function get buildingType() {
    return _buildingType;
  }
}

然后我们可以像这样使用此类: p>

We can then use this class like this :

// to dispatch the event
private function onMouseClick(e:MouseEvent = null):void {
  var iconClickedEvent:BuildingEvent = new BuildingEvent(BuildingEvent.BUILDING_HELD, buildingType);
  stage.dispatchEvent(iconClickedEvent);
}

// to listen to the event
private function init(e:Event):void {
  stage.addEventListener(BuildingEvent.BUILDING_HELD, buildingHeld);
}
private function buildingHeld(event:BuildingEvent):void {
  buildingType = event.buildingType;
}

这篇关于ActionScript 3.0-带参数的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆