How to make hero strafe (move side to side)?

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 strafe (move side to side)?

Post by sheamkennedy »

EDIT: I no longer require assistance, I spent the day working it out and think I've got it figured out. I'd share my solution but I think since I have a lot of interference from other scripts the solution would only be applicable to my own game. I'm sure doing something like this is normally a lot easier.

I want to make a code for my game so that while the key "S" is held on the keyboard my hero will be locked in to facing their current direction until the key is released.

For instance, if the hero is facing north and "S" is held, then if the player pressed another direction the hero will move in that direction while still facing north. I've tried a few ways to get this to work but haven't had any success.
Last edited by sheamkennedy on Sun Nov 08, 2015 11:30 pm, 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
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

I was trying to find if there was an easy way to do this without completely overriding hero movement, but I didn't find an easy way.
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 »

Yeah I struggled with it. I ended up doing kind of a workaround where I drew up 4 new character sprite sets. One where all the frames are of the character walking right, one walking left, walking up, and then walking down. By switching the character to these sprite sets it appears that they are facing their strafing direction when in reality they are not. This does mean that you can talk to NPCs that appear to be beside you or behind you while stafing, though I don't think there is anything wrong with this type of functionality in my game. In fact it allows for complex things like hitting a switch that appears to be beside me while simultaneously fighting of foes that appear to be in front of me.

Here's an idea of what I did in my code:
-if keypress S then

Code: Select all

if(isStrafing == false) then(
              isStrafing := true
              if(hero direction(0) == right) then(
                isStrafingR := true
              ) else if (hero direction(0) == left) then(
                isStrafingL := true
              ) else if (hero direction(0) == up) then(
                isStrafingU := true
              ) else if (hero direction(0) == down) then(
                isStrafingD := true  
              )    
            ) 
-then I proceeded to update the hero pictures depending on which of these variables was now true:

Code: Select all

            if(isStrafingR == true) then(
              if (hero by slot (0) == hero:Taras) then(
                set hero picture (0, 124)
              ) else if (hero by slot (0) == hero:Antonina) then(
                set hero picture (0, 115)
              ) else if (hero by slot (0) == hero:Rodion) then(
                set hero picture (0, 102)  
              )
            ) ... etc...
-else, at the end of the keypress code I set isStrafing and all isStrafing directions to false
-I figure doing this would work for most people's purposes.

This didn't completely work in my script due to other things like eachstep scripts, timers, and so on, so I have other little codes to handle this interference. Also I had to have a check to see which direction I was facing while strafing so when I clicked the attack button it would attack in the correct direction.

After getting this working it's actually a lot of fun. I can see this type of feature being used in shooting games and maybe some sort of fast paced puzzle games. I think this feature kind of makes up for the negatives of my game being grid-based.

This leads me to my next question which is how to override the normal arrow key movement so if I click "up" for instance it just makes me face upwards then subsequent holding or pressing of the up arrow will result in northward movement? If you can point me in the right direction it would be appreciated. I think I'll try some stuff on my own then post a new thread if I can't get it to work.
⊕ 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
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Ah, now if you want to completely override player movement, then you start with

suspend player

And then just make sure that your on keypress script checks all the arrow keys for movement, the menu keys for the menu, and the use key to use the NPC in front of you.

You will probably also need a "hero is walking" check so that you can't try to change directions in the middle of a step.
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! Unfortunately my script already has a bunch of player suspend and resumes for other tasks so I didn't want to go that route. I did however use your suggestion to make a script that does normal movement if you are currently facing the correct direction, else suspend the player and change their direction to the correct direction then resume normal movement. It seems to work. There is some change in direction midstep which can occur but it looks natural and I think I'll just keep it like that.
⊕ 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 »

(This is in reply to the original strafing question, not the turn-and-then-move question)

Actually, overriding normal movement isn't necessary at all. When the hero is stationary and the player presses a movement key you use walk hero followed by set hero direction to override the direction the hero is facing. Because the hero is now moving the normal movement keys do nothing. For example, here's a working script to strafe using the ASDW keys:

Code: Select all

# on-keypress script
plotscript, strafe, begin
  variable(dir)
  dir := hero direction(me)
  if (hero is walking(me) == false) then (
    if (keyispressed(key:a)) then (
      walk hero(me, left)
      set hero direction(me, dir)
    ) else if (keyispressed(key:d)) then (
      walk hero(me, right)
      set hero direction(me, dir)
    ) else if (keyispressed(key:w)) then (
      walk hero(me, up)
      set hero direction(me, dir)
    ) else if (keyispressed(key:s)) then (
      walk hero(me, down)
      set hero direction(me, dir)
    )
  )
end
Last edited by TMC on Tue Nov 10, 2015 4:49 am, edited 2 times in total.
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 »

Ahhw man you make it look so simple. Well I guess my script is similar but I just have a condition to suspend and resume movement as I figured the normal arrow key functionality would conflict with my script. I think I kind of got the other stuff working as well. Just trying to fix a bit of weirdness with my characters attack animations. I think I just have to make the animations all based on timers to get things working proper.
⊕ 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 »

Yes, animations can be a pain. Aside from timers the other option is to put some information in the slice extra data or elsewhere about the animations, and just iterate over all the heros/slices/whatever is animating each tick to update the animations.
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 »

Alright that's good to know. I guess I'll stick with timers since I seem to be most familiar with that. I just hope I don't run in to any situations where it slows down the game a whole lot. I suppose all my cutscenes will be done with wait commands of displaying backdrops on a tick-by-tick basis, so at least I know those major scenes shouldn't slow anything down.
⊕ 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