繁体   English   中英

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

对于 phaser3 :

item.texture.key

示例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM