Boolean / if statement help

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 = jumpTimerSPACING2/JUMP_DURATION;
println(jumpOffset);
}

Hello!

This:

  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

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?

I noticed this but I haven’t checked

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.

1 Like

You add something to the position

Make sure when position is >= target position you stop

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.

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

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”