Page 1 of 1

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

Posted: Sat May 19, 2012 7:25 pm
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.