Make an object move with another object (i guess?)

Started by Grilled, February 18, 2015, 22:17:32

Previous topic - Next topic

Grilled

This is the Gun class file:

public class Gun {
	
	private float x,y;
	private float vel;
	private int rounds;
	private float fireRate;

	
	public Gun(int rounds, float fireRate){
		this.rounds=rounds;
		this.fireRate=fireRate;
		x=Player.getX();
		y=Player.getY();
		vel=0.0f;
		
	}
	
	public void draw(){
	
				
			glPushMatrix();
			glTranslated(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();
								
	
	}
	
	public void fire(){
		
		x+=vel;
		draw();
		
		if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
			vel=5.5F;
			
			
			
		}
		
	}
}


Essentially I want the drawn bullet to not appear until the space bar is pressed, and when pressed, it should move from wherever the player is. Basically what I have now is that I pre-drew the bullet and hid it behind the player and whenever space is pressed it just moves from behind the player. The reason I have it drawn even when the spacebar isn't pressed is because when I have it so that if the space bar is pressed AND THEN I draw the bullet, it moves but it's only visible if the spacebar is pressed, as per the if statement, but I haven't thought of a way around it as of yet.

Thanks for your help!

Kai

What you want to do is not only move the bullet and keep it visible for as long as the player "holds down" the spacebar.
Therefore you would need to set some boolean flag, indicating that at some point the player pressed (pushed down and released) the spacebar button.
Once you recognize the press of the spacebar button, you would remember that by setting the boolean flag.
In your update mechanism of your render loop, you would query the boolean flag and advance the bullet for as long as that flag is true (or the bullet hits something or whatever).
Also, since pressing some key is rather an "event" then a continuous state (that you would poll each frame), you might want to use the event-mechanism of the Keyboard class.
So, you would use the Keyboard.next() method in a while loop to loop over each generated keyboard input event.
And inside the loop you would check for the spacebar button with Keyboard.getEventKey() == Keyboard.KEY_SPACE and check if it was pressed via Keyboard.getEventKeyState().
Those information I just looked up on http://ninjacave.com/lwjglbasics2.
Have a look at that.

Neoptolemus

There are a couple of issues here. The main issue is that the drawing and movement of the bullet are handled in the fire() method, which is an odd setup to have. Depending on when fire() is called, it would mean that the bullet is only drawn and moved under certain circumstances.

I wrote a side-scrolling shooter a while back, and the way I handled things was to have each game object (character, bullet, powerup etc) have a standalone draw() method, and a tick() method. tick() would handle all calculations you would want to happen on a per-frame basis, such as movement, collision detection etc.

The main game loop would keep an ArrayList of game objects and cycle through each one, calling its tick() function. Then during the draw phase it would cycle through each one and call draw(). A bit like this:

private ArrayList<GameObject> allObjects = new ArrayList();
private float deltaTime;

public void myLoop() {
   long startTime = Sys.getTime();

   for (GameObject a : allObjects) {
      a.tick(deltaTime);
   }

   for (GameObject a : allObjects) {
      a.draw();
   }

   long endTime = Sys.getTime();
   long result = endTime - startTime;
   deltaTime = (float) result / 1000.f;

}


Then your tick() method might look a little like this:

public void tick(float deltaTime) {
   x += vel * deltaTime
}


That way, the drawing and movement of the bullet is no longer tied to the player or their actions.

Bear in mind that this is a very simplified example, but it should give you an idea on how to proceed. I also am aware of the limitation of a naive loop like I've put in, but I wanted to keep things simple for now and that is a different issue altogether ;)

david37370

Don't you want to create an ArrayList of bullets in the game world? Fire() function would then add a bullet with x y of the player and speed. Then you could fire as much as you'd like, and also remove the bullets when they get too far from the world. Good luck :)
David