Preventing Keypress from continuously activating

I have my code for keypress here. What I’m looking for is a way to prevent the code from jumping straight from “isGameStart” to “isGameS1”. So that it requires you to hit the ENTER key again.

void keyReleased()
{
if (keyCode == ENTER && isGameStart)
    {
    isGameStart = false;
    isInstructions = true;
    }
if (keyCode == ENTER && isInstructions)
    {
    isInstructions = false;
    isGameS1 = true;
    }
}
1 Like

I assume isGameS1 is actually isInstructions, right? In any case, you need to use if else, not just if.

How if Else works :

//this happens all during one frame!
if (true) {//this is executed}

if (true) {
   //this is executed
} else { //this is not}

if (false) {
   //this is not
} else if (false) {
   //this is not
} else if (false) {
   //this is not
} else { //this is executed}

if (false) {
   //this is not
} else if (true) {
   //this is executed
} else if (true) { //this is not even checked, since we already found a true condition before
   //this is not!!!
}
2 Likes

might be possible using just a other sort order of the commands,
but a other logic might also help

void keyReleased() {
  if (keyCode == ENTER ) {
    if (isInstructions) {
      isInstructions = false;
      isGameS1 = true;
    }
    if (isGameStart) {
      isGameStart = false;
      isInstructions = true;
    }
  }
}

1 Like

I prefer the if-else here. If you were to re-arrange the code in the future, this bug will return. When you read the code, you should see the connection and dependency.

Kf

2 Likes