# Problem with boolean changing to false randomly

**URL:** <https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152>\
**Category:** Coding Questions\
**Created:** [December 5, 2019, 2:20pm UTC](https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152 "2019-12-05T14:20:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 5, 2019, 2:20pm UTC](https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152/1 "2019-12-05T14:20:12Z")

</div>

So I’ve made another code using if else instead of a switch, but more problems arise. For some stupid reason, whenever I press “1” on my keyboard, it sets game to false?

As you can see in the code below, I didn’t make it so that it sets game to false whenever I click 1, only when I press “p”. Same goes for the other 2 parts which I haven’t included here that change xSpeed to something different, they also set game to false, but the statements work fine (xSpeed works correctly).

If you saw my [previous post](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109), you would have noticed that I changed origxSpeed to orig cause it was too long lol. I won’t include the variables up top but please let me know if you need them, I will be happy to add them.  
I feel nervous to post this as it seems so impossible for game to be set to false unless I accidentally typed it in the “if key==1 statement”. That would be very embarrassing. So embarrassing I would delete the post (that is, if it doesn’t get deleted by someone else first lol).

Oh, and I assure you, resetGame() does **not** set game to false, I double, triple, **quadruple** checked lol.

```auto
void keyPressed() {
  if ((key == 'p' || key == 'P') && game==false) {
    game=true;
    noCursor();
    xSpeed *= 0.5;
    ySpeed *= 0.8;
    diff="Easy";
  } else {
    if ((key == 'p' || key == 'P') && game==true) {
      if (abs(xSpeed)==0.75*orig) {
        xSpeed *= 4/3.0; 
        ySpeed *= 5.0/6;
      } else {
        if (abs(xSpeed)==0.5*orig) {
          xSpeed *= 2;
          ySpeed *= 1.25;
        } else {
          if (abs(xSpeed)==1.6*orig) {
            xSpeed *= 1;
            ySpeed *= 0.625;
          }
        }
      }
    }
    game=false;
    score=0;
    setup();
    cursor();
  }

  if ((key == '1') && game==true) { 
    if (abs(xSpeed)==0.75*orig) {
      xSpeed *= 2.0/3; 
      ySpeed *= 2.0/3;
    } else {
      if (abs(xSpeed)==0.5*orig) {
        xSpeed *= 1;
        ySpeed *= 1;
      } else {
        if (abs(xSpeed)==1.6*orig) {
          xSpeed *= 0.5;
          ySpeed *= 0.5;
        }
      }
    }
    score=0;
    resetGame();
    diff="Easy";
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 5, 2019, 3:08pm UTC](https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152/2 "2019-12-05T15:08:46Z")

</div>

> [@TurboSlayer](#):
>
> game=false;

Maybe this line is inside the else??

(and outside the if inside the else section)

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 5, 2019, 6:51pm UTC](https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152/3 "2019-12-05T18:51:33Z")

</div>

? I‘m 95% sure that game will be set to false if you press any key other than ‚p‘…

Your Code goes like this :

```auto
Any Key is pressed ->
Check if the key is P ->
If not, set Game to false, Score to 0, setup() and cursor(). 

```

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 6, 2019, 8:59am UTC](https://discourse.processing.org/t/problem-with-boolean-changing-to-false-randomly/16152/4 "2019-12-06T08:59:47Z")

</div>

Lol I feel so embarrassed. You guys were right, the game=false was outside the if. If you look at the blue thing on the curly bracket you will see that it is one line before the game=false.  
 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/98407ba4cb5ae9ca05c450db180c32886f2b0afb.jpeg)
