当对所有按钮使用相同的回调或事件侦听器时,如何检查按下了哪个按钮

How to check which button was pressed when using the same callback or event listener for all of them

我有一个自定义的 class 菜单按钮。总的来说,我创建了这个按钮的对象,并在函数中使用它们。最终我想要: 当我点击按钮 1 时,显示文本 1;单击按钮 2,显示文本 2,等等...

以下是我拥有的主要和自定义 class 的代码示例:

class WeaponMenu extends Sprite{

var wpnMenuImagePath:String;
var _wpnMenuImagePath:String;

var wpnMenuName:String;
var _wpnMenuName:String;

var swordMenuBtmp:Bitmap = new Bitmap ();
var swordMenuSprt:Sprite = new Sprite();

var bowMenuBtmp:Bitmap = new Bitmap ();
var bowMenuSprt:Sprite = new Sprite();

public function new(wpnMenuImagePath, wpnMenuName) {

    super();

    _wpnMenuImagePath = wpnMenuImagePath;
    _wpnMenuName = wpnMenuName;

    createWpnMenu ();
}
public function createWpnMenu () :Void {

    if (_wpnMenuName == "Sword") {

        swordMenuSprt.x = -500;
        swordMenuSprt.y = ((Lib.current.stage.stageHeight - swordMenuBtmp.height) / 2) -150;

        swordMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
        swordMenuSprt.addChild(swordMenuBtmp);

        var swordMenuSprtX:Float = ((Lib.current.stage.stageWidth - swordMenuBtmp.width) / 2) -200;
        var swordMenuSprtY:Float = ((Lib.current.stage.stageHeight - swordMenuBtmp.height) / 2) -150;

        Actuate.tween(swordMenuSprt, 2, { x:swordMenuSprtX, y:swordMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);

        addChild(swordMenuSprt);
    }
    if (_wpnMenuName == "Bow") {

        bowMenuSprt.x = (Lib.current.stage.stageWidth - bowMenuBtmp.width) / 2;
        bowMenuSprt.y = Lib.current.stage.stageHeight + 500;

        bowMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
        bowMenuSprt.addChild(bowMenuBtmp);

        var bowMenuSprtX:Float = (Lib.current.stage.stageWidth - bowMenuBtmp.width) / 2;
        var bowMenuSprtY:Float = ((Lib.current.stage.stageHeight - bowMenuBtmp.height) / 2) -150;

        Actuate.tween(bowMenuSprt, 2, { x:bowMenuSprtX, y:bowMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);

        addChild(bowMenuSprt);
    }
    if (_wpnMenuName == "Staff") {

        staffMenuSprt.x = Lib.current.stage.stageWidth + 500;
        staffMenuSprt.y = ((Lib.current.stage.stageHeight - staffMenuBtmp.height) / 2) -150;

        staffMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
        staffMenuSprt.addChild(staffMenuBtmp);

        var staffMenuSprtX:Float = ((Lib.current.stage.stageWidth - staffMenuBtmp.width) / 2) +200;
        var staffMenuSprtY:Float = ((Lib.current.stage.stageHeight - staffMenuBtmp.height) / 2) -150;

        Actuate.tween(staffMenuSprt, 2, { x:staffMenuSprtX, y:staffMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);

        addChild(staffMenuSprt);
    }
}
}

这是来自 Main 的代码:

class Main extends Sprite {

public var swordMenu:WeaponMenu;
public var bowMenu:WeaponMenu;
public var staffMenu:WeaponMenu;

public function new () {

    super ();
    swordMenu = new WeaponMenu ("ui/swordBtn.png", "Sword");
    bowMenu = new WeaponMenu ("ui/bowBtn.png", "Bow");
    staffMenu = new WeaponMenu ("ui/staffBtn.png", "Staff");
    weaponChoose ();
}
    public function weaponChoose ():Void {

    swordMenu.buttonMode = true;
    swordMenu.useHandCursor = true;
    bowMenu.buttonMode = true;
    bowMenu.useHandCursor = true;
    staffMenu.buttonMode = true;
    staffMenu.useHandCursor = true;

    swordMenu.createWpnMenu();
    bowMenu.createWpnMenu();
    staffMenu.createWpnMenu();
    addChild(swordMenu);
    addChild(bowMenu);
    addChild(staffMenu);
    swordMenu.addEventListener(MouseEvent.CLICK, choose);
    bowMenu.addEventListener(MouseEvent.CLICK, choose);
    staffMenu.addEventListener(MouseEvent.CLICK, choose);
}

public function choose (event:MouseEvent):Void {

    if (event.currentTarget == swordMenu) {
    trace ("good"); 
    }

    swordMenu.removeEventListener(MouseEvent.CLICK, choose);
    bowMenu.removeEventListener(MouseEvent.CLICK, choose);
    staffMenu.removeEventListener(MouseEvent.CLICK, choose);

    removeChild(swordMenu);
    removeChild(bowMenu);
    removeChild(staffMenu);
}

对原始代码示例感到抱歉。

所以我想做的是检查在主按钮中按下了哪个按钮,并将此信息发送到自定义 class 并触发一个函数,该函数将为每个按钮显示正确的文本。

解决方案一:

检查 event:MouseEvent 参数的 target 属性(不是 currentTarget)——它很可能是您想要的特定按钮(它是那个或按钮的容器)。如果它是您想要的按钮(只需找出 event.target 要检查的内容),那么您需要做的就是

if (event.target == button1) { doSomething();} else if (event.target == button2) { doSomethingElse();}

如果这不起作用,可以使用函数绑定。

方案二:

Haxe 有一个名为 function binding 的功能,允许您将特定数据附加到回调。

所以你的情况是这三个按钮都连接到同一个事件侦听器响应函数,但是如果 event.target 不是你想要的按钮,那么就没有办法区分实际被点击的是什么当时——他们都调用同一个函数。 有些东西被点击了,但你不知道是什么。

所以你想要做的是,当你分配监听器时,你向函数调用添加一些关于这是哪个按钮的信息。

swordMenu.addEventListener(MouseEvent.CLICK, choose.bind(_,"sword"));
bowMenu.addEventListener(MouseEvent.CLICK, choose.bind(_,"bow"));
staffMenu.addEventListener(MouseEvent.CLICK, choose.bind(_,"staff"));

public function choose (event:MouseEvent,id:String):Void {
   if(id == "sword") { onSword();}
   if(id == "bow") { onBow();}
   if(id == "staff") { onStaff();}
}

让我解释一下这里发生的事情——通常您只需将函数名称本身传递给 addEventListener。相反,我们所做的是对其调用 "bind()"。 Haxe 中的每个函数都可以通过传递给它的任意数量的参数调用“.bind()”,这将 return 一个附加了额外参数的新函数。

看看这个:

swordMenu.addEventListener(MouseEvent.CLICK, choose.bind(_,"sword"));

作为第一个参数的_表示"pass whatever you would normally pass to this slot",然后我们将"sword"作为常量传递。

这基本上只是一种更简单的输入方式:

swordMenu.addEventListener(MouseEvent.CLICK, 
    function(e:MouseEvent):Void
    {
        choose(e,"sword");
    }
);
bowMenu.addEventListener(MouseEvent.CLICK, 
    function(e:MouseEvent):Void
    {
        choose(e,"bow");
    }
);
staffMenu.addEventListener(MouseEvent.CLICK, 
    function(e:MouseEvent):Void
    {
        choose(e,"staff");
    }
);

public function choose (event:MouseEvent,id:String):Void {
   if(id == "sword") { onSword();}
   if(id == "bow") { onBow();}
   if(id == "staff") { onStaff();}
}

效果相同,但更冗长。