Problem with on_key_press

Started by idiotbox, Sat 29/10/2005 21:35:24

Previous topic - Next topic

idiotbox

My character has a knife and I want him to strike continuously if the key is held down, but there should be a second or so in between each strike. The character just strikes repeatedly and ends up stuck in the striking position if I hold the key down. It would be even better if I could only make him strike once, even if the key is held down. Is there any way to do this?

Ashen

You should be able to use the Character.Animating property to stop it sticking.
E.g.:
Code: ags

if (keycode == 32) {
  if (cEgo.Animating == false) { // character isn't animating
    cEgo.Animate (2,3,eOnce); // or whatever
  }
}

Will mean that the character will keep playing the animation as long as the key is pressed - but will play it all the way through. To add a delay between it playing - or to make it only play once - I think you'll have to use variables and Timers.
E.g.
Code: ags

//on_key_press
if (keycode == 32) {
  if (canplay == 0) {
    cEgo.Animate (2,3,eOnce); // or whatever
    canplay = 1;
    SetTimer (1, 40); // If you want the delay version
  }
}

// repeatedly_execute
if (IsTimerExpired (1)) canplay = 0; // Obviously, for the delay
// or
if (IsKeyPressed (32) == 0) canplay = 0; // to play once, even if key is held down

I know what you're thinking ... Don't think that.

idiotbox

Hm, well that works, thanks! but the only issue is that the character strikes in the direction he is facing. So I suppose I could throw in an "if" for each direction he faces as well, because otherwise he would only strike in one direction.

Ashen

Character.Loop. Should stay the same when you change views for the animation, so you could do:
Code: ags
cEgo.Animate(cEgo.Loop, 3);


(If you're using un-used loops in the same view, you could do cEgo.Loop+8, or whatever.)

Side note:
Please read through the manual, or at least the index. The last few questions you've asked are pretty much directly answered by just reading the manual, and being familar with the various Character, Object, GUI and GUI Control properties.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk