Advertisement Jump to content
Sign in to follow this  
ecr0n

SDL Timing for Shooting

This topic is 4968 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!