how to make on_key_press trigger once only

Started by BOYD1981, Thu 25/05/2006 18:01:52

Previous topic - Next topic

BOYD1981

i'm using on_key_press to animate a character in my game but i only want the action to happen once and not to happen again once i release the key (it's causing problems with the footstep sound being played repeatedly until the sound engine crashes)
i'm using this code in the room script

Code: ags
function on_key_press(int keycode){

Ã,  if (keycode=='UP') cEGO.Animate(0, 0,eOnce,eNoBlock);


UP isn't the actual key i'm using but the game is for OROW and i don't want to give too many hints away.

any help will be greatly appreciated

Limey Lizard, Waste Wizard!
01101101011000010110010001100101001000000111100101101111011101010010000001101100011011110110111101101011

DoorKnobHandle

Use the character.Animating flag to check whether the character is currently (already) animating.

Something like this should work:

Code: ags

if ( keycode == 'UP' && !cEGO.Animating )
   cEGO.Animate ( 0, 0, eOnce, eNoBlock );

BOYD1981

that's fixed the problem of it repeating before the animation has finished but it's added a new problem, i now can't trigger the action again until the animation has stopped and i've had to put a delay in the last frame of the view to stop it repeating too quickly again.
one way to describe what i want to do is comparing to the use key in DOOM, pressing it will activate a switch or door once even if you keep the button pressed, but if you wanted you could keep pressing the use key which would trigger the door again even if it hadn't finished opening/closing.

Limey Lizard, Waste Wizard!
01101101011000010110010001100101001000000111100101101111011101010010000001101100011011110110111101101011

Ashen

You could use a variable, and IsKeyPressed(), e.g.:
Code: ags

// on_key_press:
if (keycode == 'UP' && UpPressed == 0) {
  //Do stuff
  UpPressed = 1;
}


//repeatedly_execute(_always)
if (UpPressed == 1 && IsKeyPressed('UP') == 0) UpPressed = 0;
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk