• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to code a sprint mechanic (similar to Mother 3) with 8 way movement?

G

gaterskater69

Guest
I'd like to start off by saying that I am very very new to coding and making games, and I'm trying very hard to learn the basics of making an RPG (heavily inspired by the Mother 3 mechanics).

At the moment I'm trying to create a sprinting mechanic, just a simple "if you hold down shift you will run faster and the sprite will change".

I've got an 8 way movement mechanic down but I'm not sure how to code in sprinting with that ( plus a sprite change when doing so).

1610847097741.png
this is the coding i've used for the 8 way movement.

(I'm more of an artist than all this so this isn't my strong suit heh...)
 
First, please put your code inside of the [code][/code] tags, rather than screenshotting it, because with screenshots if we want to alter your code we have to type the whole thing out rather than copying and pasting it. As @rytan451 said, you need to increase the spd variable when shift is being held. A good way of doing this is by using a multiplier on spd. Something along these lines:
Code:
var _mod_spd = spd*sprint;
moveX = lengthdir_x(_mod_spd,dir);
moveY = lengthdir_y(_mod_spd,dir);
Now we need to create the sprint variable in the Create Event:
Code:
sprint = 1;
Finally, change the value of sprint between 1 and whatever multiplier you want the speed to be depending on whether shift is being held (in the Step Event, before the _mod_spd stuff, otherwise sprint will update after you've already done the calculation and it won't change anything):
Code:
if (keyboard_check(vk_lshift)) {
   sprint = 2;
}
else {
   sprint = 1;
}
Finally, you said you want to alter the sprite if sprinting. One way to do this is to duplicate your switch statement and put one inside an if (sprint == 1) { } and put the other inside an else { } statement after that, then modify all the sprites in the else { } switch statement to be the "sprinting" ones.
 
Top