# Firing a bullet

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I am attempting to make a top-down shooter in C# and XNA. My problem comes with the bullet handling. I think that using the generic List<T> is probably the best idea, filling it with a bullet objects. So my question starts here: once the key (space) is hit for the bullets to be fired, what should my next step be? I was thinking something along the lines of
List<Bullets> bullets = new List<Bullets>();
KeyboardState kState = Keyboard.GetState();

if (kState.isKeyDown(Keys.Space))
bullets.add(new Bullet(ship1)); //parameter shows which ship fired from


But then in my Update method and my Draw method, what would I need to do? Basically I would need to figure out how to have the bullets move forward (easy I would think, just decrement the y-values), have them be on the screen only for like 5 seconds and then vanish, and check if they collide with anything. Also, the issue of having more than one set of bullets needs to be addressed too. Any suggestions?

##### Share on other sites
That's pretty much it. I'd probably have the Bullet class have its own update function, and during your game's update function, you can traverse the list and called Bullet::Update on each item. In Bullet::Update, move the bullet forward, check for collision, respond to collision if one exists, and destroy the bullet when it goes off screen.

##### Share on other sites
Quote:
 Also, the issue of having more than one set of bullets needs to be addressed too.

It occurred to me that you could solve this by not solving it.
Since you already have a reference in each bullet to its owner, you can get away with one bullet list for all the bullets in the game.

##### Share on other sites
Quote:
Original post by pulpfist
Quote:
 Also, the issue of having more than one set of bullets needs to be addressed too.

It occurred to me that you could solve this by not solving it.
Since you already have a reference in each bullet to its owner, you can get away with one bullet list for all the bullets in the game.

Thats a smart idea, however, I think you misunderstood me. I do plan on only having one list for all of the bullets, what I meant by my statement was needing to draw and update each Bullet object in the list, when there were more than one bullet being fired.

Sorry for the confusion.

##### Share on other sites
That confused me more, are you talking about how to iterate through the list?

##### Share on other sites
Most games will redraw the entire screen each frame so every graphical objects currently inside the screen has to be redrawn each frame. No matter if they have moved or not.
I guess you could keep a rect for each object and invalidate only those that has moved since last frame, before calling repaint but I'm not sure that is a feasible option.
I'm still a bit confused wether that is your beef or not

##### Share on other sites

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628715
• Total Posts
2984360

• 24
• 11
• 10
• 13
• 14