LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Grilled on March 02, 2015, 03:20:31

Title: Bullet disappears when the Space bar is pressed
Post by: Grilled on March 02, 2015, 03:20:31
OK. Here's my problem:

Basically, I want to my bullet do what bullets in video games usually do; shoot whenever the button specified is pushed. Well I have more or less half of that done.

Here's my fire method:
public void fire(){


if(Keyboard.isKeyDown(Keyboard.KEY_SPACE) && isEmpty()==false){
fireRate=5.5f;
firing=true;


}
System.out.println(firing);


}


My update method:

public void update(){
Bullet.x+=fireRate;

if(clip.size()==0){
spawnBullets();
}



for(Bullet bullet: clip){


bullet.draw();

}

}


My spawn bullets method:
public void spawnBullets(){


for(int i=0; i<numberOfBullets; i++){

clip.add(new Bullet());
}

}


My bullet class:


public class Bullet {

public static float x,y;

public Bullet(){

x=Player.getX();
y=Player.getY();


}

public void draw(){



glPushMatrix();
glTranslatef(x,y,0);
glBegin(GL_QUADS);
glColor3f(0.4f, 0.5f, 0.6f);
glVertex2d(-8,0);
glVertex2d(8,2);
glVertex2d(8,5);
glVertex2d(-8, 6);

glEnd();
glLoadIdentity();
glPopMatrix();


}

}


And finally my game loop:
while(!Display.isCloseRequested()){



cam();


if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)==true){
gun.update();

gun.fire();



}

player.drawPlayer();

Time.update();
wave.update();


boundingBox();

Display.update();
Display.sync(60);

}


As of right now, the code that I have just spawns the bullet at the players location and only appears and moves if i have the space bar held down. How could I change this so that the bullet gets drawn at the players location and stays drawn.

Thanks!  :o
Title: Re: Bullet disappears when the Space bar is pressed
Post by: Exempt on March 02, 2015, 13:05:14
Your bullet spawn method didn't paste in properly but I'm guessing your issue is you set the fire_rate what space is down, do you set it back to 0 somewhere else?
Title: Re: Bullet disappears when the Space bar is pressed
Post by: Grilled on March 02, 2015, 13:43:28
yeah, the fireRate is declared as 0 and then when the space bar is pressed the fireRate=5.5f