Hi everyone!
I have a problem with looping, now I know how to constrain values and stuff with constrain() but I am not sure how to stop an infinite loop when I am using a keyPressed function.
I basically want the 'shooting effect', so when I press spacebar a bullet is shot, but it's not visible before pressing and it just appears every time the spacebar is pressed.
This is my bullet class:
class Bullet1 {
float d; //Diameter
int x = 100;
int y = 100;
Bullet1(float tempD) {
d = tempD;
}
void display() {
rectMode(CENTER);
stroke(0);
fill(175);
rect(x,y,d*5,d*5);
}
void keyPressed() {
if(key == ' ') {
y = y - 4;
}
}
}
And this is my setup class:
Pistol pistol;
Bullet1 bullet1;
void setup() {
size(200,200);
smooth();
pistol = new Pistol(1);
bullet1 = new Bullet1(2);
}
void draw() {
background(255);
//Pistol initialisation
pistol.display();
pistol.setLocation(pistol.x,pistol.y);
//New Bullet1 initialisation
bullet1.display();
bullet1.keyPressed();
}
Hope someone can help! :)
1