We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to get bullets to shoot out. When i press the mouse they only flash quickly, what can i do to fix this issue
final float SPEED = 2;
final float BULLET_SPEED = 4;
final int GUN_WIDTH = 30;
final int GUN_HEIGHT = 50;
int centerX,centerY;
float posX,posY;
float trianglePos1X,trianglePos2X,trianglePos3X;
float trianglePos3Y;
float bulletDiam = 10;
float bulletX, bulletY;
float x,y;
void setup(){
size(500,500);
centerX = width/2;
centerY = height/2;
posX = centerX - GUN_WIDTH/2;
posY = height - GUN_HEIGHT;
}
void draw(){
background(200);
drawGun();
moveGun();
}
void moveGun(){
if(keyPressed && (key == CODED)){
if((keyCode == LEFT) && ( posX > 0 )){
posX = posX - SPEED;
} // bracket for LEFT
if((keyCode == RIGHT) && ( trianglePos2X < width)){
posX = posX + SPEED;
} // bracket for RIGHT
} // bracket for CODED
} // bracket for moveGun
void drawGun(){
trianglePos1X = posX;
trianglePos2X = posX + GUN_WIDTH;
trianglePos3X = posX + GUN_WIDTH/2;
trianglePos3Y = posY - GUN_WIDTH;
fill(255);
rect(posX , posY, GUN_WIDTH,GUN_HEIGHT);
triangle( trianglePos1X ,posY, trianglePos2X , posY , trianglePos3X, trianglePos3Y);
}
void mousePressed(){
drawShot();
moveShot();
}
void drawShot(){
bulletY = posY;
bulletX = trianglePos3X;
int i = 0;
while ( i < 320) {
ellipse (bulletX, bulletY + i, bulletDiam, bulletDiam);
i = i + 40;
}
}
void moveShot(){
bulletY = bulletY - BULLET_SPEED;
}
Answers
You were drawing the bullet in mousePressed() - this is probably not what you want.
I tried to fix it. Have a look:
Notice that the bullet is now always drawn inside draw(). You should do all your drawing in draw()! When the mouse is pressed, the bullet's position is reset.
Why post here and start a new Question? That'll confuse everybody.