LibGDX Best way to handle shooting bullets..

Ahoy!

I’m curious. What would be the best way to handle bullets within a LibGDX game? Specifically using a bullet class, play screen, and textures/rectangles. I’m generally asking about spawning the bullets when shooting.

Thanks!

  • A

I guess you are talking about a 2D game right?
It would be nice to know what kind of game it is.
Top down or a plattformer?
Are you able to aim/shoot everywhere or are you limited to some directions?
Basicly you need to create a new Bullet and set it’s position to the Shooters position + some distance in the direction you are aiming.

If it is possible, that there are many Bullets you could think about pooling.

Sorry, you are right.

It’s just a side scrolling, platformer (I guess…) more or less. 2D, yes. Basically just shoot left or right following the players Y position as well.

& Yes, I know the basic idea of the bullet. I just need to know the best way for actually spawning the bullets.

Are you using scene2d? Then just create an Actor bullet, set its position and add it to the Stage.
Are you using box2d? Then create the bullet body, fixture, set its position and add it to the box2d world.

If you don’t use any of this things, just create your bullet, set its position and add it to your world, which could be for example an ArryList which contains all objects in your world.

Don’t think to complex, a bullet is just an entity, like the player or anything else. You only need to take care about the position, which depends on the players position and direction. The rest is the same.

I’m generally specifically asking for its spawning method. Not how to create it, I’ve got that… for example, I need to know the best way to handle spawning individual ones that on remove only remove a certain one. :slight_smile:

Spawning is nothing but creating, setting the rigth position and adding it to the things to be drawn and updated.

If you mean “when to spawn”, there are a few things you need to take care about:

  • The firerate: It defines how many bullets can be shot (max.) in 1 second.
  • Non automatic weapons? User has to press shoot again and again. Automatic weapons? User can hold down shoot-button.

And a few more things, but that depends on the actual game.

If you mean “what kind of bullet to spawn”, just “ask” your player what weapon he is holding and if a weapon can have different bullettypes, ask it which bullettype is actually loaded.

For the “remova a certain one”, i guaess you only need to remove the one which collided with something right? So this should be handled by your collision detection, which adds the dmg to the thing that was hit (if a wall can’t get dmg just ignore that) and then remove the bullet from the things to be drawn and updated.