Check for the duration of a collision

Hi I am trying to add a little leniency to my collisions. Specifically I am making a racing game and want to allow players to be able collide with a wall for time n before calling a kill function.

Ideally i would just use a setTimeout() function and see if we the sprites are still colliding in n seconds. Or better, check some bool in n seconds, and toggle that bool to false when the collision event stops.

There are a lot of sprites on screen so the body.checkCollision properties mostly return true all of the time.

the sprite in question does not collide with the wall in question so body.touching properties are always returned false.

Iā€™m thinking of doing something like the sudo code below, but it seems a bit inelegant and would depend not on time but one the number of loops through the update cycle. which is obviously meh.

/pseudo code
var playState = {
update: function() {

    isCrashingBool = false;
    game.physics.arcade.overlap(wallSprite, car, wallExposure, null, this);
    if (isCrashingBool) {
      count +=1;
      if(count === countLimit){
        // do something
      }
    }
  }
}

function wallExposure() {
  isCrashingBool = true;

}

Thanks in advance folks.

var crashTime = 0;
var LIMIT = 500;

function update () {
  var isCrashing = game.physics.arcade.overlap(wallSprite, car, wallExposure, null, this);

  if (!isCrashing) crashTime = 0;

  if (crashTime > LIMIT) {
    // ā€¦
  }
}

function wallExposure() {
  crashTime += this.time.physicsElapsedMS;
}

Hi Samme
This is great, just what i was looking for. Thanks for your help with this.

db

1 Like