GameMaker: Studio

GameMaker: Studio

View Stats:
gamer3192 Sep 13, 2016 @ 5:25pm
Sprint button
Does anyone know how to make a sprint button that when held down it will speed your character up a "x" amount? I've tried a few different ways but its either the effects of pressing the sprint button stack (goes REALLY fast) or just goes to the amount but doen't go back when released. Any help will be appreciated.
< >
Showing 1-6 of 6 comments
GreatDane Sep 13, 2016 @ 10:33pm 
hey, so if you set a variable for the movement speed in the create event (movement=1) then in the control to move you make the hspeed and/or vspeed equal to that number(hspeed=movement) then all you have to do is change that number when your sprint key is pressed so when pressed movement=2 when released set movement=1 again
Diveyoc Sep 14, 2016 @ 5:37am 
If you're using code in your step event rather than the drag and drop feature, you can add something like this:

if keyboard_check(ord('B'))
{
motion_add(image_angle, 10);
if speed > 10 speed = 10;
}

This way you will sprint at a speed of 10, but not exceed a speed of 10.
Not sure if this is a good, optimal, profesional, or whatever way of doing it, but heres how I'd probably approach it...

I always keep track of an objects max_speed, acceleration, braking, momentum loss, and whether or not its moving with a boolean variable.

So for example maybe something like...
max_speed = 5; acceleration = 0.5; braking = 0.5; momentum_loss = 0.3 moving = false;

To control and manage my speed every step I'd limit it from going over my maximum speed, limit it from going below zero, and if I'm not moving I'd subtract my momentum loss from it.

So...
// Don't feel like typing it but here I'd have my code to check movement keys and // either increase the speed or decrease it if I'm accelerating or braking. if (speed > max_speed) then speed = max_speed; if (moving = false) then speed -= momentum_loss; if (speed < 0 ) then speed = 0;


If I was going to add sprinting to this I'd probably use a boolean variable to flag whether or not I'm sprinting and have a variable to keep track of how much faster I go while sprinting. I'd probably change my max_speed to be base_max_speed and then have a total_speed amount to factor everything off of.

So...
sprinting = false; sprint_boost = 5;

Then my speed management every step would probably look something like this...
// Don't feel like typing it but here I'd have my code to check movement keys and // either increase the speed or decrease it if I'm accelerating or braking. total_speed = base_max_speed + (sprint_boost * sprinting); if (speed > total_speed) then speed = total_speed; if (moving = false) then speed -= momentum_loss; if (sprinting = false && speed > base_max_speed) then speed -= momentum_loss; if (speed < 0) then speed = 0;


There may be some errors or typos in that and I don't have the code where I check what keys are pressed to flag sprinting or moving as true or false nor checking if I'm pressing keys to accelerate or brake, but overall it should be functional, or close to it.

I haven't had a need to do sprinting or boosting of any kind in any of my projects so far, but the above is roughly how I'd approach it.
DerexXD Sep 14, 2016 @ 2:21pm 
Originally posted by gamer3192:
Does anyone know how to make a sprint button that when held down it will speed your character up a "x" amount? I've tried a few different ways but its either the effects of pressing the sprint button stack (goes REALLY fast) or just goes to the amount but doen't go back when released. Any help will be appreciated.
make a action where you press a button and it sets the speed to something faster than your normal speed

try this:
speed=(#greater than your original speed)
DerexXD Sep 14, 2016 @ 2:22pm 
Originally posted by DEREX WQ:
Originally posted by gamer3192:
Does anyone know how to make a sprint button that when held down it will speed your character up a "x" amount? I've tried a few different ways but its either the effects of pressing the sprint button stack (goes REALLY fast) or just goes to the amount but doen't go back when released. Any help will be appreciated.
make a action where you press a button and it sets the speed to something faster than your normal speed

try this:
speed=(#greater than your original speed)
DeathInc Sep 14, 2016 @ 7:59pm 
So, this:
Keypress event: speed = speed + bonusX;
keyup event: speed = speed - bonusX;

Where bonusX = your run boost. Make bonusX a global variable and you can adjust the value using equipment/powerups/adjustable-piece-of-gear
< >
Showing 1-6 of 6 comments
Per page: 1530 50

Date Posted: Sep 13, 2016 @ 5:25pm
Posts: 6