Jump to content
  • Advertisement
Sign in to follow this  
ecr0n

SDL Timing for Shooting

This topic is 4877 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

Hello. I'm creating a space arcade game with SDL and i'm having trouble with the timing for shooting. The keyboard updates through every game loop and it makes it so that if i press a key to fire it shoots multiple bullets. How could i time it?

Share this post


Link to post
Share on other sites
Advertisement
I generally have a variable holding the last time (as returned by SDLGetTicks()) the weapon was fired, and in the mainloop, check that variable each time the user hits the fire key. Something like this...


int lastFireTime = 0;
#define REFIRE_TIME 250 /* milliseconds */

/* in the mainloop */
if( /* fire key is pressed */ )
{
if( (SDLGetTicks() - lastFireTime) > REFIRE_TIME )
{
doFiringStuff();
lastFireTime = SDLGetTicks();
}
}



Of course, depending on how your game is structured, you might want to do the actual checking of refire time somewhere else, with different variables per ship, per player, per weapon, whatever.

Share this post


Link to post
Share on other sites
Thank you.

It worked perfectly, I had been having this trouble with other various projects too.

Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!