Page 1 of 1

How to make hero revert to "walking" sprite once k

Posted: Thu Mar 05, 2015 1:15 am
by sheamkennedy
I have the following code which makes my hero "run" when I hold down the "S" key. It both changes the characters speed and their sprite changes to their "running sprite." The code is here:

Code: Select all

plotscript, On Keypress Handler 0, begin
  if(hero is walking(me) == false) then(
    set hero speed(me, 4)
    #Set leader to walking
      If (hero by slot (0) == hero:Taras) then(
        set hero picture (0, 44)
      ) else if (hero by slot (0) == hero:Antonina) then(
        set hero picture (0, 47)
      ) else if (hero by slot (0) == hero:Rodion) then(
        set hero picture (0, 37)  
      )
    
  ### S ###    
      if(key is pressed(key:S)) then(
        set hero speed(me, 5)
        #Set leader to running
          If (hero by slot (0) == hero:Taras) then(
            set hero picture (0, 99)
          ) else if (hero by slot (0) == hero:Antonina) then(
            set hero picture (0, 100)
          ) else if (hero by slot (0) == hero:Rodion) then(
            set hero picture (0, 98)  
          )
      )
   )...
This is good and all, but if I click the "S" button while stationary my hero changes to look as if they are running even though they are standing still.

To attempt to fix this I made a reset code which was called in my autorun while loop, it looked like this:

Code: Select all

script, resetRun, begin
  if(hero is walking(me) == false) then(
      #Set leader to walking
      If (hero by slot (0) == hero:Taras) then(
        set hero picture (0, 44)
      ) else if (hero by slot (0) == hero:Antonina) then(
        set hero picture (0, 47)
      ) else if (hero by slot (0) == hero:Rodion) then(
        set hero picture (0, 37)  
      )
  )
end
It didn't work as intended... As I see it, it's checking if I'm in motion every tick and resetting my sprite if I am not. But instead what is happening is it keeps reseting my sprite even when I am in motion. I can not understand why.

Can anyone suggest an alternate way of handling this? Or point out any mistake if I made one?

Posted: Thu Mar 05, 2015 3:56 am
by Gizmog
Take all of this advice with a grain of salt, because I'm not sure I totally understand the problem. What you might try to do is break it down into two parts somewhere within a while loop or someplace they're both gonna checked a lot, something like...

Code: Select all

script,SprintToggle,begin
If (KeyVal (Key:S) >> 1)
then (
        if (CheckTag (Sprinting))
        then (SetTag (Sprinting,off)
        else (SetTag (Sprinting,on)
        )
end

script,FixPictures,begin
#If the hero is walking...
if (HeroIsWalking (me))
then (
        ..check to see what pictures to do...
        if (CheckTag (Sprinting))
        then (
                #Change the pictures to the sprinting ones
               )
        else (
                #Change the picture to the walking ones
               )
         )

#If the hero ISN'T walking
else (
       #Set the picture to the walking ones
       )
end
I imagine there'll probably be a little bit of a delay in the pictures being right because of that "Scripts run at the end of a tick" thing TMC always talks about, but it might work... or it might not! This kind of "Hero is Moving" stuff is something that's always hard for me to wrap my brain around.

Posted: Thu Mar 05, 2015 4:38 am
by sheamkennedy
I took from that what I could and switched over to using a tag. It kind of works. What I mean is it shows a frame of sprinting spring and flickers between that and the normal sprite while character is moving. Not sure why this happens but it gave me a good idea nonetheless. I figured it could save me time by not having to draw sprinting sprites but instead I could copy the walking sprite and just add speed-lines moving behind them. I tried it and the effect is nice so thank you. I'll consider this matter resolved.

Posted: Thu Mar 05, 2015 4:47 am
by Gizmog
If it's flickering, it could be the KeyIsPressed instead of KeyVal? KeyIsPressed only cares that the button is pressed or not, not whether or not it's a new key press so 18.2 times a second KeyIsPressed is going to do what it wants to do. Glad that the matter's resolved, though!

Posted: Thu Mar 05, 2015 6:41 am
by sheamkennedy
Gizmog wrote:If it's flickering, it could be the KeyIsPressed instead of KeyVal? KeyIsPressed only cares that the button is pressed or not, not whether or not it's a new key press so 18.2 times a second KeyIsPressed is going to do what it wants to do. Glad that the matter's resolved, though!
I actually still am using key is pressed. Didn't end up trying keyval, just ended up using a global variable to control things. I liked the look of it so didn't take things further but it's good to know for other things I may run into later. I'll show you a quick video of how it turned out.

https://www.youtube.com/watch?v=Gbcg6zB-fe8

Oh and the "R" that is blinking in the corner was what I was originally using to verify whether I was running or not, I'll be getting rid of that in the game.

Posted: Thu Mar 05, 2015 9:44 am
by TMC

Code: Select all

script,SprintToggle,begin
If (KeyVal (Key:S) >> 1)
then (
        if (CheckTag (Sprinting))
        then (SetTag (Sprinting,off)
        else (SetTag (Sprinting,on)
        )
end
This isn't going to work, because "keyval() > 1" is true once key-repeat kicks in. So whether you use keyval or keyispressed here you'll get flicker. There's no script command to check for a true new keypress, though it's easy to write a script to check it by remembering whether the key was down last tick. I really need to add better keypress script commands, I don't know why I've put it off for years and then had to explain keyval 30 times.

I imagine there'll probably be a little bit of a delay in the pictures being right because of that "Scripts run at the end of a tick" thing TMC always talks about
No, they run at the beginning, (which is why you can momentarily turn on suspendplayer to block certain keypresses).

But you are right that if you wanted to do this properly with different sprites while moving and running you would need to work around this limitation of the script interpreter. Luckily, I already wrote a script to do that:
http://rpg.hamsterrepublic.com/ohrrpgce ... ding_still
You could have either used just the eachstep script on that page to reset the sprites to the walking one, or modify your existing resetRun script (called every tick) by changing "hero is walking(me) == false" to "hero will move == false"

Posted: Thu Mar 05, 2015 4:09 pm
by sheamkennedy
Thanks for clarifying things.