Jump to content

change sprite velocity on collision


sparksflyupwards
 Share

Recommended Posts

I have a set of pins set up on a board and I want the ball to get a specific velocity when it collides with a pin in order to guide it to a predetermined destination. basically every time the ball collides with a member of the pins group I do a check to see if the ball should be falling left or right and then force that velocity on the ball, this is designed to ensure that the ball falls in the correct predetermined slot.

 

at the moment I'm using the phaser overlap function:

 
        this.physics.overlap(coins, pins, moveCoin, null, this);
 
where move coin looks like this:
 
 
function moveCoin(coin, pin){
 
    if(shouldMoveRight(coin))
        coin.body.velocity.x=100;
    else
        coin.body.velocity.x=-100;
}

 

Now my issue is that this function seems to only work some of the time and not others. This is pretty confusing and I do not quite understand why that would be the case. Is there a better way for me to implement this or is there something i'm doing wrong? I've tried using the collide function as well with the same effect. Some help will be massively appreciated! 

 

 

Link to comment
Share on other sites

My guess is that the current velocity of the coin may be greater than 100 and it will not stop moving immediately

 because of physics.

 

For example if velocity is 200 and it is set to -100 that would happen gradually since its mass is still in movement.

thanks Ernesto i tried to add velocity to the sprite rather than just change it. that improved it somewhat. there is a bigger issue though, the moveCoin function is only called while i'm dragging the sprite :S!

when the sprite is in "free fall" and collides with the pins the collide function is not called at all. does this have something to do with dragEnabled? thanks for your help so much man :)

Link to comment
Share on other sites

thanks Ernesto i tried to add velocity to the sprite rather than just change it. that improved it somewhat. there is a bigger issue though, the moveCoin function is only called while i'm dragging the sprite :S!

when the sprite is in "free fall" and collides with the pins the collide function is not called at all. does this have something to do with dragEnabled? thanks for your help so much man :)

1. What is the contents of shouldMoveRight()?

2. Which version of Phaser you are using? In newer version, you have to select the physics engine you are using. Enable physics for objects.

 

Always have a look to API documentation and examples. Here is doc on Arcade Engine. See the part under title 'collide(object1, object2, collideCallback, processCallback, callbackContext) → {boolean}'

 

Here is a decent example on it.

 

EDIT:

Personally I use collide. It never failed for me.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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