Jump to content

[JS] moveToPointer - object trembling when colliding with mouse


ricardocouto8
 Share

Recommended Posts

Hello everyone,

 

I started using Phaser today and I must say I am impressed. Well done guys!

 

I made a simple image rotate and move towards the mouse, as explained in the examples "accelerate to pointer" and "move towards object". What happens though is that when that image collides with the mouse (meaning image coordinates = mouse coordinates) the image stops and starts trembling awkwardly. Any way to make it stop facing the last position before colliding?

 

My code:

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {    game.load.image('player', 'assets/player.png');}function create() {    player = game.add.sprite(400, 300, 'player');    player.anchor.setTo(0.5, 0.5);}function update() {    if (game.physics.collide(player, this.game.input.activePointer.circle))    {        player.velocity.x = 0;        player.velocity.y = 0;    }    else    {    	player.rotation = game.physics.moveToPointer(player, 60, this.game.input.activePointer);    }}

Thanks!

Link to comment
Share on other sites

Hi - the problem is that collide won't work with Sprite vs. a Circle object, so the velocity will never be reset which means it will carry on moving to the pointer, which will be like tiny amounts (probably 0.x of a pixel) causing the stuttering.

 

What I would suggest is you can either use activePointer.circle.contains(sprite.x, sprite.y) or you could position a sprite on the pointer and then use collide against that (you can set it to renderable = false to stop it displaying if you need).

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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