1

I try to make shooter game on C# with SFML.NET, but I can`t imagine how to make an ability to shoot more than 1 bullet, because now I have just one null-object of bullet-class, and when player presses Space key this object gets link to the new bullet.

So, I have the Bullet-class, null-object

public static Bullet bullet = null;

and condition

if (Keyboard.IsKeyPressed(Keyboard.Key.Space)) 
{
   if(bullet == null) 
    bullet = new Bullet(t, p.rect.Left, p.rect.Top, p.reverse);
}

When bullet reaches the wall or enemy bullet object gets equated to null. The problem is to make ability to shoot more bullets before this bullet reaches the wall or enemy (and disappear). I think this is not a good solution to make null-objects for every possible pullet, because then we have limited amount of possible bullets.

Ken White
  • 123,280
  • 14
  • 225
  • 444
CssHammer
  • 66
  • 7

3 Answers3

5

I would not suggest creating a list of bullets, but rather an array of bullets.

When the key is pressed you could add another bullet to the array and run update logic on all bullets in the array.

This will also allow you to loop back to the beginning to re-use memory instead of infinitely growing a list.

So for example in your keyPressedEvent:

If space on keyboard is pressed
    Increment bulletCounter
    if(bulletCounter > length of bullets array)
        set bulletCounter = 0;
    Set bullets[bulletCounter] = new Bullet(parameters)
austin wernli
  • 1,801
  • 12
  • 15
  • Should I create an array of null-objects or just an empty array? – CssHammer May 08 '15 at 18:34
  • An empty array. You could add in a counter for which bullet you are currently on, and increment that for each time the key is pressed. However, you will have to set this variable back to zero once it is greater than the size of the array – austin wernli May 08 '15 at 18:37
  • And how should I update every existable bullet? I used to update my bullet that way in the cycle: bullet.update(time); – CssHammer May 09 '15 at 08:32
  • I second using an array of bullets – waltmagic May 11 '15 at 15:22
  • In your game loop, you would loop through your bullets `foreach(var b in bullets]` and update each bullet accordingly. – austin wernli May 11 '15 at 15:32
3

You should look into the basics of game programming. For this situation you would use an array to contain N number of bullets.

Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
0

Bullet only equals null once here also static need not be used here...remove the if(bullet == null) and replace public static Bullet bullet = null; with List bullets; Something like this should work:

    List<Bullet> bullets = new List<Bullet>();

KeyPressed event

    if(Keyboard.IsKeyPressed(Keyboard.Key.Space))
    {
        bullets.Add(new Bullet(t, p.rect.Left, p.rect.Top, p.reverse));
    }

Instead of setting the bullet to null when it hits the wall, get rid of/Dispose of it, maybe bullets.Remove(). No sense in keeping it right? Hope this gets you where you need to go.

Edit: It might actually be smart to limit the number of bullets allowed to be created by creating an array instead of a list as another person mentioned. What if the space bar key got stuck?

Bullet[] bullets = new Bullet[1000];

Maybe something like that...

waltmagic
  • 631
  • 2
  • 9
  • 22