Jump to content

Point of click inside a sprite


daemiean
 Share

Recommended Posts

Hi,

 

Is there any way to obtain the point inside the sprite where you are clicking? What I want is to apply force in the opposite direction to the center of the sprite, so I would need to know where I clicked, sorry if is a stupid question but I'm new with phaser.

 

Thanks!

Link to comment
Share on other sites

When the pointer is over the sprite, you can check sprite.input.pointerX() and sprite.input.pointerY() to give you where the sprite was clicked. These values are zeroed on the top left of the sprite so some calculation may be needed to get it relative to the centre of the sprite (not sure if these values take into account anchor, for instance). So something like this:

function update() {  if (sprite.input.justPressed(game.input.activePointer)) {    var clickPosition = new Phaser.Point(sprite.input.pointerX(), sprite.input.pointerY());    // ... do something here with clickPosition to determine desired velocity...  }}
Link to comment
Share on other sites

If for some reason this is broken, the next best alternative I can suggest is to calculate where the pointer is via game.input.activePointer.worldX and .worldY and then comparing it to the sprite's position. If you take the difference of the pointer's world position from the sprite position you end up with the same values as above.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...