Hi all,
I'm making a button mash mini game but there's a problem when players hold down the button. It acts like a turbo button and repeatedly makes IsKeyPressed(32) default to true. I suppose I only want this to be true when players let go of the spacebar after having pressed it down. That way, it requires someone to press, let go, press, let go... i.e. button mashing. Any ideas how I can set this up?
in on_key_press function I have
if (IsKeyPressed(32))
{
Score += 1;
if ( Score >= 5 )
{
Score = 0;
character[1].Frame += 1;
character[1].y += 10;
}
}
Use a timer? You could set it to a small amount, and after its done, it would run your code. Like this:
if (IsKeyPressed(32))
{
if (IsTimerExpired == 1){
Score += 1;
if ( Score >= 5 )
{
Score = 0;
character[1].Frame += 1;
character[1].y += 10;
SetTimer(1,10);
}
}
}
I haven't coded in a while, and I just woke up, so forgive me if this isn't even one bit right. :)
Also, under SetTimer, I put 10 because its super short, although if you want long 1 second delays, put 40.
It's much easier:
if (IsKeyPressed(32)) {
while(IsKeyPressed(32)) {
Wait(1);
}
Score++;
if (Score>4) {
Score = 0;
character[1].Frame += 1;
character[1].y += 10;
}
}
Thanks guys, I tried using KhrisMUC's solution and it didn't allow for animations to continue if the player happened to continuously hold down on the spacebar. As long as it was pressed down, the game stopped processing.
However, I have found a different way to implement this, I will detect mouse clicks with eMouseLeft events instead of spacebar presses.
Cheers!
-Reid
Ah, here's a non-blocking way:
bool down;
//rep_ex
if (IsKeyPressed(32)) {
if (!down) {
down=true;
// do stuff
}
}
else down=false;