What I'm trying to do

Have bullet's spawn in the center ( or close enough, the exact number can be played with after ) of my player sprite.

What's going wrong:

They're spawning super weirdly. The first one always spawns at the bottom left corner. Than they spawn all over the place though not completely random. If I shoot on only the right side of the screen, bullets don't spawn on the left side.

How I'm doing it:

Creating a New Bullet and Managing them , Methods: Creating a new Bullet and Managing them Method

Method Placement: Method Placement

Drawing (In Batch): Drawing the Bullets


I know that I'm not using LibGDX arrays, or objects, or array of objects, or sprites for the bullets. ( I used sprites for the player ) though I was going through confusing complications and this strategy with batch drawing seems inefficient but simplified for my development ability.

I also haven't made a bullet disposal/reset system yet. I'm still working on this part first.

Anyone know what I'm doing wrong or a solution?

EDIT: ManageBullets is just so the bullet's go up once they spawn. ( Shooting )

  • \$\begingroup\$ What you are trying to do with manageBullets()? \$\endgroup\$ Feb 15, 2016 at 5:48
  • \$\begingroup\$ @HamzaHasan Read edit \$\endgroup\$
    – Kyle Meade
    Feb 15, 2016 at 6:27
  • \$\begingroup\$ Man I don't know libGDX, but something I know that you can never get animated shooting effect through for-loop. It is too fast for that.. Use game loop for that instead \$\endgroup\$ Feb 15, 2016 at 6:34
  • \$\begingroup\$ @HamzaHasan that's the whole point, the for loop is fast. I'm not using the for loop for animation. I'm using it to quickly animate all the bullets that are currently active. It's in the game loop. It's quickly pushing all the bullets in the game once up a bit in the game loop. \$\endgroup\$
    – Kyle Meade
    Feb 15, 2016 at 6:36
  • \$\begingroup\$ can you post the snap of this issue (gameplay)? \$\endgroup\$ Feb 15, 2016 at 6:40

1 Answer 1


Assuming that your activeBullets member is initialized with 0:

In newBullet you increment the activeBullet variable before adding a new bullet to the vector. So your first "initialized" bullet with x and y coordinates will be on index 1. But in manageBullets() your loop goes from 0 to activeBullets-1 ( i < activeBullets ). That means your bullet at index 0 will never be set with player coordinates.

Thats the reason your first bullet is in bottom left corner ( 0 / 0 ).

Your code should be

public static void newBullet(){
   bulletX[activeBullets] = player.getX(); // first bullet = bulletX[0]
   bulletY[activeBullets] = player.getY();
   activeBullets++; // next bullet will be bullet[1]

Also note that the bullet will spawn on the left side of your player-sprite ( because the sprite has its left side at x ). So you need to add half the player-sprite width to your bullet x and subtract half bullet-sprite width

Pseudo code:

bulletX[activeBullets] = player.getX() + ( playerSprite.getWidth() / 2 ) - ( bulletSprite.getWidth() / 2 )

Hopefully last edit:

You should handle the player movement before adding new bullets. You spawn the bullet first and then move the player, so the bullet will spawn at the point where the player was in the last frame. Thats the reason for your strange behaviour, that the bullet spawns "randomly".

So first move, then shoot!

  • \$\begingroup\$ Wow thank you. You fixed the issue perfectly. My Input was after setting the mouseX and mouseY so I thought i'd be fine. But moving it around and moving the new bullet increment up worked perfectly. And yes, active bullets did start at 0. I'd up vote but I don't have enough "rep". \$\endgroup\$
    – Kyle Meade
    Feb 15, 2016 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .