I’m making a side-scrolling fighting game (kind of like Street Fighter) that uses A and B for attacks, START for jumping (weird, I know), and the directional pad for moving. So, I want to make it so that running can be triggered by double-taping in a direction, and when you stop moving, you default back to walking. But I don’t see any blocks that could help with that, and I don’t know how I would code it either. If you could help that would be great!
Welcome! I suggest that when a directional button is pressed, you could start a timer / create a value that ticks down by 1 every millisecond. If that same button is pressed while the timer is greater than 0, then its been double pressed within the time allotted, and you can trigger the running!
erm, actually- according to my calculations, the game your thinking of is “streets of rage”. (sorry lol)
The first thing is a design decision. You have said you want a double arrow or d-pad to signal a run.
- Do you see your sprite walking with the first touch, stop when released, then run with the second rouch?
- Do you want your sprite to start walking and continue walking for some interval when the button is released such that if you are walking and you press again you run.
- Do you want to run in different directions once you start running. Meaning if you run left then press right are you running or walking now?
I am guessing you want the 1 option because the 2 from a design point of view makes the sprite somewhat uncontrollable. And for 3 you probably want to change to a walk when you change directions. Maybe not?
So we need to keep track of some small amount of time. There are a couple ways to do that. The best way I think is to set up a simple timer.
All we are doing is subtracting 1 from our timer every game update. That is the simplest timer possible.
Unfortunately we will need some complexity in the way we handle the joystick. We can create event handlers for button down and button up events as follows:
So look at what that does. When up is pressed we will test if the timer is going and if it is a double press in the same direction. If that is true we can start running. We know that we were walking because the timer is running. Else we will start walking and reset the timer. At the end we save the direction we were going in.
We must also add a button released so that we stop walking or running on releasing the button.
So how does this behave? When we press a direction (in this case up) we will start walking and start our timer. If we release the button and press it again immediately we will start running. The timer is running so it is greater than zero and we saved the previous direction for comparison.
Notice that if we are running and we release and press the up button we will go back to walking. That is because the timer is only reset when we start walking. How would you change it so that we can run and keep running?
That is one direction handled. Your job is to add the other 4 directions. But also you must think about what happens with the diagonal movements! Will those also work? What do you need to do to make them work if they don’t?
You can try it here.