[JS] moveToPointer - object trembling when colliding with mouse

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);    }}


Share this post

Link to post
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).

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.