Hi everyone, I’m doing my Processing homework and I’ve got my object but I can’t get it to shoot. Can someone tell me the correct code please? I have the following so far:
int x = 30;
int y = 30;
void setup(){
size(500,500);
}
void draw(){
background(0);
fill(255,56,99);
ellipse(x,y,60,60);
}
void keyPressed(){
if (key == 's') y = y + 8;
if (key == 'w') y = y - 8;
}
void bullet (){
float gunX = 30;
float bulletX = 30;
float bulletY = 30;
ellipse(gunX, y,60,60);
bulletX = gunX +200;
ellipse(bulletX,bulletY,60,60);
if (key == 'd') x = x+200;
}
of course in a real game you can shoot multiple bullets from different positions that fly on the same time. So a few bullets are visible and animated at the same time
This is the code that I have. I’m able to shoot my bullet but unable to shoot one bullet at a time. Would I have to use another Boolean variable so I can fire a bullet only when it leaves the screen?
int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
boolean bulletFlies=false;
float bulletX = 20;
float bulletY = 20;
Yes that would totally work!
You could also directly call a function
boolean can_shoot(){...}
that would tell you if the bullet left the screen (using the built-in variables width and height) and call that function when the right key is pressed to ensure that besides pressing the space bar, no other bullet is left on screen.
Be careful though as the first bullet will always be considered as ‘on screen’ with that because it initial position is (20, 20) which is on screen, so one easy way to work around that (though it is not proper at all) is to init the bullet position off screen, or you could just add a condition in the can_shoot() function to check whether the bullet is flying (which is cleaner).