Jump to content

Sprites Collide and cause a nudging effect


CaptKiwi
 Share

Recommended Posts

Hi Guys,

 

I'm really enjoying Phaser and getting great results.

 

I have a question about collisions:

 

When two sprites hit each other, the sprites nudge each other, knock them back a little. This would normally be fine, but it is causing the sprite to slowly move in another direction because of the nudge. Is there a way to turn that off the nudging effect so that they collide without any force?

 

Cheers,

CaptKiwi

Link to comment
Share on other sites

    function update() {        game.physics.collide(sprite1, sprite2, collisionHandler, null, this);    }    function collisionHandler (obj1, obj2) {        obj1.body.velocity.x = 0;        obj2.body.velocity.x = 0;    }

just set the velocity to 0

 

 

 

Works perfectly :-)

 

Thanks 4ucai!!!

Link to comment
Share on other sites

Just to add that if you don't want a sprite to move when it's collided with, just do this:

sprite.body.immovable = true;

(you can toggle that boolean at any point in your game too)

 

And just to clarify - an immovable true sprite can still be adjusted via velocity / acceleration like normal, it just doesn't receive any impact velocity.

 

If you want a sprite to NEVER move, including when setting velocity on it, you'd use:

sprite.body.moves = false;
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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