获取在 Phaser 中被点击的精灵的名称

Get the name of the sprite that has been clicked on in Phaser

我需要为我的纸牌游戏创建带有点击事件的精灵。单击卡片时,我希望回调函数知道单击了哪张卡片。为此,用于精灵的图像名称就足够了。我有什么办法可以做到这一点吗?我想象它会像这样工作:

  card.events.onInputDown.add(actionSelectedCard(???), game);

  function actionSelectedCard(cardName){
  ...
  }

或者由于移相器事件系统的性质,这甚至不可能吗?

找到答案了。 添加事件时,您传递使用 "this".

单击的按钮
card.events.onInputDown.add(actionSelectedCard, this);

然后我可以获得与

一起使用的精灵的名称
function actionSelectedCard(card){
    alert(card.key);
}

对于移相器 3:

item.texture.key

示例:

this.ship = this.physics.add.sprite(WIDTH/2,HEIGHT-100,'ship');
this.ship.texture.key // ship