简体   繁体   中英

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".

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 :

item.texture.key

Example :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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