# Boolean / if statement help

**URL:** <https://discourse.processing.org/t/boolean-if-statement-help/24809>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 22, 2020, 12:35am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809 "2020-10-22T00:35:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimchi](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kimchi](https://discourse.processing.org/u/kimchi)\
**Post date:** [October 22, 2020, 12:35am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/1 "2020-10-22T00:35:37Z")

</div>

Hi there, I’m trying to make a program that has a “fly” that jumps around the 4 dots of the clock. I have to use if statements in order to make the fly to move and I think I have a good idea of how to do it, but now I’m a little stuck since the fly moves off the screen. I have the condition where if jumpTime\>=JUMP\_DURATION that it sets jumpTime to 0 and in theory should stop the fly from moving. However, when I print this statement it’ll wait until the fly moves off the screen and then print false.

//clock global variables  
final int HOUR\_HAND = 150;  
final int MINUTE\_HAND = 200;  
final int DIAM = 25;  
final int SPACING = 100;  
float hourAngle = 0;  
float minuteAngle = 0;  
float hourSpeed = 0.01;  
float minuteSpeed = 0.02;

//fly global variables  
int flyX;  
int flyY;  
boolean isJumping = false;  
boolean atTop;  
boolean atLeft;  
boolean jumpUp;  
boolean jumpDown;  
boolean jumpLeft;  
boolean jumpRight;  
int jumpTimer;  
final int JUMP\_DURATION = 20;  
final int FLY\_DIAM = 30;  
int jumpOffset;

void setup() {  
size(500, 500); //make a 500x500 canvas  
flyX = width/2 + SPACING;  
flyY = height/2 + SPACING;  
atTop = flyY \< height/2;  
atLeft = flyX \< width/2;  
}

void draw() {  
background(255); //make background white  
drawDots();  
drawHands();  
moveHands();  
drawFly();  
moveFly();  
frameRate(10);  
}

void drawHands() {  
int centerX = width/2;  
int centerY = height/2;  
float hourHandX = cos(hourAngle)\*HOUR\_HAND+width/2;  
float hourHandY = sin(hourAngle)\*HOUR\_HAND+height/2;  
float minuteHandX = cos(minuteAngle)\*MINUTE\_HAND+width/2;  
float minuteHandY = sin(minuteAngle)\*MINUTE\_HAND+height/2;

strokeWeight(5);  
line(centerX, centerY, hourHandX, hourHandY);  
line(centerX, centerY, minuteHandX, minuteHandY);  
}

void drawDots() {  
strokeWeight(1);  
fill(0, 220, 220); //make dots blue  
ellipse(width/2+SPACING, height/2+SPACING, DIAM, DIAM);  
ellipse(width/2-SPACING, height/2+SPACING, DIAM, DIAM);  
ellipse(width/2-SPACING, height/2-SPACING, DIAM, DIAM);  
ellipse(width/2+SPACING, height/2-SPACING, DIAM, DIAM);  
}

void moveHands() {  
hourAngle = (hourAngle+hourSpeed)%TWO\_PI;  
minuteAngle = (minuteAngle+minuteSpeed)%TWO\_PI;  
}

void drawFly() {  
strokeWeight(1);  
fill(200); //make fly grey  
ellipse(flyX, flyY, FLY\_DIAM, FLY\_DIAM);  
}

void keyPressed() {

if (isJumping) {  
return; //leave the function  
} else if (!isJumping) {  
jumpUp = false;  
jumpDown = false;  
jumpLeft = false;  
jumpRight = false;  
if (keyCode == UP) {  
if (!atTop) {  
jumpUp = true;  
isJumping = true;  
jumpTimer = 0;  
}  
} else if (keyCode == DOWN) {  
if (atTop) {  
jumpDown = true;  
isJumping = true;  
jumpTimer = 0;  
}  
} else if (keyCode == LEFT) {  
if (!atLeft) {  
jumpLeft = true;  
isJumping = true;  
jumpTimer = 0;  
}  
} else if (keyCode == RIGHT) {  
if (atLeft) {  
jumpRight = true;  
isJumping = true;  
jumpTimer = 0;  
}  
}  
}  
}

void moveFly() {  
/_if (isJumping) {  
setOffset();  
}_/  
if (jumpUp) {  
setOffset();  
flyY = flyY - jumpOffset;  
jumpTimer++;  
isJumping = true;  
if (jumpTimer \>= JUMP\_DURATION) {  
isJumping = false;  
jumpTimer = 0;  
println(jumpTimer \>= JUMP\_DURATION);  
}  
} else if (jumpDown) {  
setOffset();  
flyY = flyY + jumpOffset;  
jumpTimer++;  
isJumping = true;  
if (jumpTimer \>= JUMP\_DURATION) {  
isJumping = false;  
jumpTimer = 0;  
}  
} else if (jumpLeft) {  
setOffset();  
flyX = flyX - jumpOffset;  
jumpTimer++;  
isJumping = true;  
if (jumpTimer \>= JUMP\_DURATION) {  
isJumping = false;  
jumpTimer = 0;  
println(jumpTimer\>=JUMP\_DURATION);  
}  
} else if (jumpRight) {  
setOffset();  
flyX = flyX + jumpOffset;  
jumpTimer++;  
isJumping = true;  
if (jumpTimer \>= JUMP\_DURATION) {  
isJumping = false;  
jumpTimer = 0;  
}  
}  
}

void setOffset() {  
jumpOffset = jumpTimer_SPACING_2/JUMP\_DURATION;  
println(jumpOffset);  
}

```auto

```

---

<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:** [October 22, 2020, 9:17pm UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/2 "2020-10-22T21:17:46Z")

</div>

Hello!

This:

```auto
  flyX = width/2 + SPACING;
  flyY = height/2 + SPACING;

```

sets the fly’s **start position**. It should therefore BE IN `setup()`.

It shouldn’t be repeated or stated anywhere else in your Sketch.

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![kimchi](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kimchi](https://discourse.processing.org/u/kimchi)\
**Post date:** [October 22, 2020, 11:05pm UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/3 "2020-10-22T23:05:28Z")

</div>

Oh awesome, thank you! Now since I’ve changed it when I press either left or up then my fly is continuing to move past the specified dots on the clock. Should I be trying to change my if-statements under the keyPressed() function?

---

<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:** [October 22, 2020, 11:24pm UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/4 "2020-10-22T23:24:12Z")

</div>

I noticed this but I haven’t checked

---

<div class="post-metadata">

**Author:** ![kimchi](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kimchi](https://discourse.processing.org/u/kimchi)\
**Post date:** [October 23, 2020, 12:44am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/5 "2020-10-23T00:44:44Z")

</div>

Yeah I’ve updated my code, but I’m still having the same problem. I’ve noticed with my if(jumpTime \>=JUMP\_DURATION) that when I print the value of it that it turns false when it should be true by the time the fly hits the dot on the clock. I’m unsure of what I should be changing here.

---

<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:** [October 23, 2020, 1:04am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/6 "2020-10-23T01:04:36Z")

</div>

You add something to the position

Make sure when position is \>= target position you stop

---

<div class="post-metadata">

**Author:** ![kimchi](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kimchi](https://discourse.processing.org/u/kimchi)\
**Post date:** [October 23, 2020, 1:17am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/7 "2020-10-23T01:17:15Z")

</div>

Yeah I tried to do that by using jumpOffset. Where jumpOffset = SPACING\*jumpTimer/JUMP\_DURATION. I try to add or subtract jumpOffset to flyX and flyY depending on what direction i’m jumping. And when jumpTimer \>= JUMP\_DURATION that it sets jumpTimer to 0 and then the jumpOffset calculation equals 0 and flyX or flyY +/- 0. In my head this would mean that the fly should stop moving.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 23, 2020, 1:53am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/8 "2020-10-23T01:53:21Z")

</div>

- [Studio.ProcessingTogether.com/sp/pad/export/ro.9LZizxKsIAY4F](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9LZizxKsIAY4F)

---

<div class="post-metadata">

**Author:** ![kimchi](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kimchi](https://discourse.processing.org/u/kimchi)\
**Post date:** [October 23, 2020, 2:32am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/9 "2020-10-23T02:32:15Z")

</div>

Sorry I’m not too sure what I should be looking at in the link you’ve sent me

---

<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:** [October 23, 2020, 8:13am UTC](https://discourse.processing.org/t/boolean-if-statement-help/24809/10 "2020-10-23T08:13:20Z")

</div>

> [@kimchi](#):
>
> I try to add or subtract jumpOffset to flyX and flyY depending on what direction i’m jumping

so when flyX increases you can also check whether it reached it position and then stop

show your entire code when you want real help and mark the relevant lines with “// qqq”
