Jump to content

Playing an animation based on mouse position


mayfray
 Share

Recommended Posts

Hi everyone,

 

I've been playing with Phaser for a few months, but this is my first post. I'm currently using v. 2.0.6

I know I'm probably missing something obvious here, but I'm trying to play a sprite animation based on the position of the mouse. The sprite is the the center of the screen and can hit projectiles coming toward it onClick. If the player clicks a projectile on the right side of the screen, I want the sprite to lift up its left arm (the sprite is facing you).

 

Here is the code I wrote (which freezes the game onClick):

 

if (game.input.activePointer.isDown) {
    if (activePointer.x > player.body.x) {
player.animations.play('left');
}
 
else if (activePointer.x < player.body.x) {
player.animations.play('right');
}  
  }
 
else {
player.animations.play('still');
}   
 
 
I would really appreciate any advice you might have! Thanks!
Link to comment
Share on other sites

If that's your code then the reason the game is 'freezing' is likely because activePointer is undefined. You need to call game.input.activePointer at all times unless you actually create a reference to it, like:

var activePointer = game.input.activePointer;

It's really beneficial to use the developer tools in your browser (usually opened by pressing F12) and checking the console for errors.

Link to comment
Share on other sites

If that's your code then the reason the game is 'freezing' is likely because activePointer is undefined. You need to call game.input.activePointer at all times unless you actually create a reference to it, like:

var activePointer = game.input.activePointer;

It's really beneficial to use the developer tools in your browser (usually opened by pressing F12) and checking the console for errors.

Yes, that was the issue. It works now. Thanks so much!!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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