Sign in to follow this  
adamsko

Sprite + physics: changing sprite's coordinates.

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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.