XNA Projectiles

Started by
3 comments, last by Alexandre Laframboise 7 years, 8 months ago

Hiya, I'm learning the ropes on how to make a basic top-down shooter in C# through XNA/MonoGame as a means to improve my grasp on general game development.

I now got a basic flying ship in position and able to move through inputs, but now I'm not too sure how to cope with adding bullets with a limit on how many shots can be fired at any given time. Any tips on how that can be done? And is it better to have one object per class?

Advertisement

Maybe a list of bullet class? Where bullet class has a position for each bullet, a 2D velocity vector, and maybe it's collision object. Not sure how you're handling bullet collision. You could probably do circular collision where you just measure whether two objects' circles overlap. The bullet would likely be so small that a circle would work fine. The other object might not conform to a circle as well. You could maybe make the circle a little extra tight on that side, or even use multiple circles depending on the object. There are several other ways you could do collision, point within a box for example.

It would seem rare to me that you would ever want one object per class for anything except something like a game object manager where there's only one per game.

You could have some sort of list of bullets, and remove them from the list once they've collided with something or gone off-screen.

If you want to limit how quickly you can fire (so you can't fire 3 shots in 0.05 seconds, you might have a timer somewhere (e.g. inside the player).

Decrement the timer with the frame duration every tick, and only allow the player to shoot if the timer isn't already running. Then when you shoot, set the timer running.

You could also have a combination of the two -- you can only have x bullets active at the same time, and they have to be fired with at least y delay.

Hello to all my stalkers.

When learning game design myself, I made a clone of Geometry Wars, so I've gone through the same issues you're going through with programming.

One way that I was able to limit the amount of projectiles fired from the ship was to create a bullet class that had a static variable 'count.' The static property will allow the value of the variable to persist through all instances of the class.

In the class's constructor, I incremented count and when the bullet was destroyed (either by collision or a time limit) I decremented count.

Then, in the method that would fire the bullets, I just did a quick check against the value of bullet.count and if it was less than the maximum I had defined, allow a new bullet to be created. You'll still need to store each bullet in a list (to iterate through and check collision) but this will give an easy way to keep track of exactly how many are allowed to spawn.

Also,

You could have some sort of list of bullets, and remove them from the list once they've collided with something or gone off-screen.

If you want to limit how quickly you can fire (so you can't fire 3 shots in 0.05 seconds, you might have a timer somewhere (e.g. inside the player).

Decrement the timer with the frame duration every tick, and only allow the player to shoot if the timer isn't already running. Then when you shoot, set the timer running.

You could also have a combination of the two -- you can only have x bullets active at the same time, and they have to be fired with at least y delay.

This is very good advice to set a timer to limit how frequently a bullet is spawned. Otherwise you'll have a bullet spawned every frame, if not every tick, and you'll quickly hit your limit.

E: clarity

Thanks a lot for the info!

Raptor, you seem to be new to the forums, so welcome~

You guys explain very well. I'll focus on learning how to declare a list of custom classes and try to follow your advice.

This topic is closed to new replies.

Advertisement