[C++] Block Space Key for 1 second (SOLVED)

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

[C++] Block Space Key for 1 second (SOLVED)

Post by vitinho444 »

Original Post:

Code: Select all

Hey guys ;)

So, im making a little 2D Engine on c++ and yesterday i added a 4 way shooting.

So, it works good, but when i press 1 time the space bar, it shoots the max bullets..(Thank god i have a limit) xD

What i need is a sort of timer.. that blocks the space key for 1 second interval so the player only shoots 1 bullet per second

Thanks a lot ;)
Solution:

Ok guys, this works for C++ but i think it works for other's, halls helped me working on this.


So first declare a int named timer or something and equal it to 0:

Code: Select all

int timer = 0;
Then in the main loop:

Code: Select all

if(timer > 0)
					{
						timer = timer - 1;
					}
					if(key[KEY_SPACE] && timer < 1) //the key you want to block 
					{
						//FUNCTION THAT SPACE BAR WILL CALL
						timer = 30; //Interval of the blocking, this depends on the fps of your game, mine is good at 30, just test it yourself
					}
And its done :D

If you have any questions just ask.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Advanced Help and Support”