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);
}