Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Reid on Wed 15/08/2007 05:58:00

Title: How do I prevent button presses from repeating?
Post by: Reid on Wed 15/08/2007 05:58:00
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;
}
}
Title: Re: How do I prevent button presses from repeating?
Post by: R4L on Wed 15/08/2007 12:05:09
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.
Title: Re: How do I prevent button presses from repeating?
Post by: Khris on Wed 15/08/2007 13:01:18
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;
    }
  }
Title: Re: How do I prevent button presses from repeating?
Post by: Reid on Sun 19/08/2007 00:55:36
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
Title: Re: How do I prevent button presses from repeating?
Post by: Khris on Sun 19/08/2007 12:19:37
Ah, here's a non-blocking way:

bool down;

//rep_ex

  if (IsKeyPressed(32)) {
    if (!down) {
      down=true;
      // do stuff
    }
  }
  else down=false;