0

I have this function which detects the collision between to objects. It is called within a Ticker (FPS 60). The if statement will run as long as the two objects are together. I think this has something to do with the ticker and it running the if every frame. What would be the best way to fix this so that for example when the two objects collided the person gets one point instead of four or ten.

function collDec(){

    var minDistance = 10 + 10;
    var xDist = circle.x - arrow.x;
    var yDist = circle.y - arrow.y;
    var distance = Math.sqrt(xDist*xDist + yDist*yDist);
    if (distance < minDistance) {
        Lpoints.text = "Points: " + ++pointsAm;  
        //console.log("HIT"); 
        var dingSound = createjs.Sound.play("sound/ding.mp3"); 
            //reset(); 
    }  
} // End of collDec
Patashu
  • 21,443
  • 3
  • 45
  • 53
logtech
  • 5
  • 5

1 Answers1

0

Have an int on each object, recently collided

If a collision occurs, set both recently collided on both objects to 2

At the start of every frame, decrement recently collided on all objects by 1 to a minimum of 0

If a collision occurs and recently collided is 1 or higher* on both objects, do not add points/play a sound but still increment recently collided.

*I think 'exactly 1' may be fine too. It seems to only matter in 'three balls collide simultaneously or near simultaneously' cases.

Patashu
  • 21,443
  • 3
  • 45
  • 53