BounceX Behaviour

Hi,

The effect I am trying to achieve, is to bounce the sprite off the player if the sprite has a certain amount of hit points above 0. I just want the sprite to move jump back slightly and then keep moving towards the player, regardless of the direction it bounces from the player.

I have tried the following bounce setting with various values between 0 and 1 and in the negative as well, but I only get one of two effects: the sprite runs off infinitely in the opposite direction, the sprite doesn’t move and keeps coming towards the player.

  caulis.forEach(cauli => {
    var caulisprite = this.cauligroup.create(cauli.x, cauli.y-18, 'cauli');
    caulisprite.hitpoints = 10;
    caulisprite.invulnerabilityTimer = 0;
    caulisprite.setBounceX(1);
  });

image

Are you using arcade physics or matter?

If you are using matter physics, there is no setBounceX method.

Assuming you are using arcade physics, since the bounce value is between 0 and 1, try something like 0.25 and tell us what happens.

Also, make sure that you added caulis as a physics group, not just a regular one. I.E. var caulis = this.physics.add.group();

@UltimateProGrammer the group definitely is a physics group. If I set the bounce value to 0.25, it is the same the sprite moves in the opposite direction until it hits another object.

Try giving your sprites drag.
e.g.

caulisprite.setDrag(i,n);

Where i and n should be between 100-1000 in my experience. Play around with it until it feels right.

Thanks, I’ll keep playing around with this, but at the moment it doesn’t seem to be the solution. I’m still getting the sprite either accelerating to the end of the screen or the drag causes the sprite to stop moving at all. I’ve been trying with the bounce and the drag, but not much luck.

Should I be setting the Y value of drag? I am just setting cauli.setDragX().

Try lowering the drag or raising the bounce or both. I will see if I have time to try this on an example.

Whatever setting I try it seems to behave in the same way, the sprite runs off until it hits another object. The thing is that if I switch off bounce, drag and have nothing happening to the sprite in the collision callback function it still does the same so I am not sure if something else needs to be changed somewhere?

That sounds strange.

If I have the collider below without a callback function, it behaves in exactly that way.

this.physics.add.collider(this.cauligroup, this.player);

For that you need something like

var sprite;
var desiredVelocityX = 60; // speed * (direction to player)

if (sprite.body.velocity.x < desiredVelocityX) {
  sprite.setAccelerationX(60);
} else if (sprite.body.velocity.x > desiredVelocityX) {
  sprite.setAccelerationX(-60);
} else {
  sprite.setAccelerationX(0);
}

Does the if statement go in the callback function that is called when the collider is activated or am I filtering all the sprites with incorrect velocity in the update method and then applying the new acceleration to all of them?

In update(), because the collision happens very quickly but the enemy needs to gradually change its speed.

I’m not having much luck with that. I seem to get roughly the desired effect with the following.

        if(!this.bouncing) {
          this.bouncing = true;

          this.enemyBounceBackTimer = this.time.addEvent({
            delay: 500,
            callback: function() {
                if(sprite.hitpoints > 0) {
                  sprite.setVelocityX(sprite.body.velocity.x * -1);
                  this.bouncing = false;
                }
            },
            callbackScope: this,
            loop: false
          });
        }

That should do fine.