How to make hero revert to "walking" sprite once key release

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

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

Post 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?
Last edited by sheamkennedy on Thu Mar 05, 2015 1:16 am, edited 2 times in total.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post 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.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post 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.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post 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!
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post 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.
Last edited by sheamkennedy on Thu Mar 05, 2015 6:43 am, edited 1 time in total.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post 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"
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Thanks for clarifying things.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Post Reply