Jump to content

Sprite + physics: changing sprite's coordinates.


adamsko
 Share

Recommended Posts

Hello. I've noticed that many people have a similar problem.
If sprite has physics enabled: how to change its position without interfering with physics system?
It's a famous 'fly off the screen' problem. If position is changed, physics system is using new coordinates to continue its work, hence sprite's surprising behavior.
Let's say,that sprite is moving and when it's clicked it should move 100px on the right and then continue its movement), maybe with a different speed. How to achieve that?
My solution is:

sprite.body.moves = false;object.sprite.x += 100;

... and then in game's update() function:

function update() {  sprite.body.reset(sprite.x, sprite.y);  sprite.body.velocity.x = 10; // new velocity  sprite.body.moves = true;}

I understand, that physics system must be after one update, otherwise switching between

body.moves = false

and

body.moves = true

will not have a chance to work.

 

After that sprite is translated 100px in the right, plus it has new velocity.
The question is: is there a proper Phaser way to achieve that without splitting this operation on instructions executed before and after update() function?

 

It's seems like a common need and yet it takes that little extra effort to achieve desired effect :)

Link to comment
Share on other sites

  • 7 months later...
  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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