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

时间:2016-02-05 10:58:58

标签: haxe openfl

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

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

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

抱歉原始代码示例。

所以我要做的是检查主要按下哪个按钮,并将此信息发送到自定义类并激活一个函数,该函数将显示每个按钮的正确文本。

1 个答案:

答案 0 :(得分:4)

解决方案1:

检查target参数的currentTarget属性(不是event:MouseEvent) - 它可能就是您想要的特定按钮(它或者是按钮& #39; s容器)。如果它是你想要的按钮(只需要找出要检查的event.target),那么你需要做的只是

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

如果不起作用,则有功能绑定。

解决方案2:

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

所以你的情况是你有这三个按钮都连接到同一个事件监听器响应函数,但如果event.target不是你想要的按钮,那么就没有办法区分什么&# 39;当时实际上已被点击 - 它们都调用相同的功能。点击了 Something ,但你不知道是什么。

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

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()"通过传递给它的任意数量的参数调用它,它将返回一个附加额外参数的新函数。

所以看看这个:

swordMenu.addEventListener(MouseEvent.CLICK, choose.bind(_,"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();}
}

哪个会有相同的效果,但更详细。