简体   繁体   English

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

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

I need to create sprites dynamically with click events for my card game.我需要为我的纸牌游戏动态创建带有点击事件的精灵。 When clicked on a card, I want to the callBack function to know which card was clicked on.单击卡片时,我希望回调函数知道单击了哪张卡片。 For that, that name of the image used for the sprite would be enough.为此,用于精灵的图像名称就足够了。 Is there any way for me to do this?我有什么办法可以做到这一点吗? I imagine it to work something like this:我想它的工作原理是这样的:

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

  function actionSelectedCard(cardName){
  ...
  }

Or is that not even possible due to the nature of phasers event system?或者由于移相器事件系统的性质,这甚至是不可能的?

Found the answer.找到了答案。 When adding an event you pass the button that was clicked with "this".添加事件时,您传递用“this”单击的按钮。

card.events.onInputDown.add(actionSelectedCard, this);

I can then get the name of the sprite being used with然后我可以获得正在使用的精灵的名称

function actionSelectedCard(card){
    alert(card.key);
}

For phaser3 :对于 phaser3 :

item.texture.key

Example :示例:

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