Game score via collision detection increment

Zubair Ali :

So i have a game where if my user collides with an image it will increase the score by 1. However because i use this method of collision detection and an if statement to increment my score, during the duration of the collision the score will go up by about 30 as the collision method detects it colliding multiple times as they pass through each other. How would i stop it from incrementing by more than one each time. Here is my code:

void draw () {  
  if (gameMode == Active) {
    if(crash() == false) {
      drawBackground();
      textSize(32);
      fill(22,100,8);
      text("Score: " + score ,20,40); //calls the drawBackground method
      alien1.update();
      alien2.update();  //constantly calls the move and render method for the alien and defender
      alien3.update();
      user1.render();
      Burger.update();
      if(Bcrash() == true) {
        if(Bcrash() == false) {
          score = score + 1; 
        }
      }
    } else {
      gameMode = End;
      textSize(32);
      fill(22,100,8);
      text("Game Over, press 'r' to restart",150,200);
    }  
  }
}

boolean Bcrash() {
  return user1.crash(Burger));
}

// Burger.class (Editor's note: I guess it's User.class)
public class User {
  boolean crash(Burger A) {   
    return(abs(x-A.x)<=30) && abs(y-A.y)<=30;
  }
}
Bryan :

Check to see if collision is false before allowing it to add another number.

something like:

Boolean collisionInProgress = false;

if(collision == true && collisionInProgress == false){
  score = score+1;
  collisionInProgess = true;
}

…loop…

if(collision == false){
  collisionInProgess = false;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169187&siteId=1