How do I prevent button presses from repeating?

Started by Reid, Wed 15/08/2007 05:58:00

Previous topic - Next topic

Reid

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
Code: ags

if (IsKeyPressed(32))
  {    
		Score += 1;		
		
		if ( Score >= 5 )
		{
		  Score = 0;
		  character[1].Frame += 1;
			character[1].y += 10;
		}
	}

R4L

Use a timer? You could set it to a small amount, and after its done, it would run your code. Like this:

Code: ags

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.

Khris

It's much easier:

Code: ags
  if (IsKeyPressed(32)) {
    while(IsKeyPressed(32)) {
      Wait(1);
    }
    Score++;		
    if (Score>4) {
      Score = 0;
      character[1].Frame += 1;
      character[1].y += 10;
    }
  }

Reid

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

Khris

Ah, here's a non-blocking way:

Code: ags
bool down;

//rep_ex

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

SMF spam blocked by CleanTalk