Sign in to follow this  

XNA Projectiles

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by Velocity Raptor

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this