Making something animate while a key is being held...

Started by De-Communizer, Wed 23/12/2009 17:12:57

Previous topic - Next topic

De-Communizer

I've been looking through, but not thus far been able to find in the forum already (though I'd have thought there'd be something, it seems like such a likely problem for people to come across). In "repeatedly_execute", I've got:

if (Thrust>0) Thrust = 0;

     if ((IsKeyPressed(eKeyUpArrow))&&(Thrust<100)) {
      Thrust+=100;
      player.ChangeView(2);
      player.Animate(1, 3, eRepeat, eNoBlock);
   }


What it does is make sure that if the up key is being held, Thrust is 100, and it reverts to 0 when not being held. The problem is, when it animates, it sticks on the first frame of the animation, until I release the key, and it starts animating just when I don't need it! How can I make it animate when it's held, and revert to its normal loop when I let go?

Thanks for your help!

monkey0506

#1
The problem is that you're calling Animate several times every second which is resetting the animation, but the good news is you're not that far off. Try doing this instead:

Code: ags
  if ((IsKeyPressed(eKeyUpArrow)) && (!Thrust)) { // (!Thrust) is a shortcut for (Thrust == 0)
    Thrust = 100; // the highest you're setting it to is 100 so there's no need to use +=
    if (player.View == NORMAL_PLAYER_VIEW) { // make sure the player's view hasn't been changed yet
      // replace NORMAL_PLAYER_VIEW with the view name or number of the appropriate view
      player.LockView(2); // using LockView allows us to temporarily change the view, instead of permanently with ChangeView
    }
    if (!player.Animating) player.Animate(1, 3, eRepeat, eNoBlock);
    // by making sure the player isn't already animating we make sure not to reset the animation
  }
  else if ((!IsKeyPressed(eKeyUpArrow)) && (Thrust)) { // (Thrust) is a shortcut for (Thrust != 0)
    // we should re-evaluate both conditions here to make sure they both failed
    // (i.e., the key has been released and thrust hasn't been reset yet)
    Thrust = 0;
    player.UnlockView(); // this will stop the player animating as well as restore the normal view
  }


Hope that helps. :)

Just FYI, the reason I suggest re-evaluating both conditions is because in this way we can use Thrust as an indicator whether or not the view has been locked and character set animating. If only one of the conditions is false (i.e., the key is not pressed OR thrust is non-zero) then the overall condition would fail as well. This would cause the Thrust variable to be unnecessarily reset every loop back to 0 (if the key is pressed Thrust will be set to non-zero), and it removes the ability for us to use that as an indicator. So to be sure that Thrust is set to 100 AND the key isn't being pressed we must check both of them again. ;)

Crimson Wizard

Try this:

Code: ags

if (Thrust>0) Thrust = 0;

     if ((IsKeyPressed(eKeyUpArrow))&&(Thrust<100)) {
      Thrust+=100;
      if (!player.Animating && player.View == 2 && player.Loop == 1)
      {
         player.ChangeView(2);
         player.Animate(1, 3, eRepeat, eNoBlock);
      }
   }


EDIT: Whoops, monkey_05_06 was seconds before me :)

monkey0506

#3
Crimson, you're checking "player.View == 2" before calling player.ChangeView...so unless the player's normal view was already set to 2 then it would never be called. If the normal view is set to 2 then the ChangeView call is completely superfluous. Also, as I indicated with my code De-Communizer is most likely meaning to use LockView, not ChangeView.

ChangeView is for permanently changing a character's view. For temporary changes (i.e., running an animation) use LockView instead. ;)

Edit: @Crimson (below): I didn't mean to sound brash, so sorry if I did. I was just pointing out that there were some flaws that jumped out at me when I saw your code. I've posted plenty of silly things in my time here by not testing my code before I go and toss it out there. I didn't test my code here, so there's a possibility I might have gone and overlooked something. So no hard feelings I hope. :=


SMF spam blocked by CleanTalk