Bullet disappears when the Space bar is pressed

Started by Grilled, March 02, 2015, 03:20:31

Previous topic - Next topic

Grilled

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

Exempt

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?

Grilled

yeah, the fireRate is declared as 0 and then when the space bar is pressed the fireRate=5.5f