• 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 do you do a double press to dash

B

Braeden Hammill

Guest
I'm coding a platformer where you press the D key to move right and A for left.
I want to make it where if you go left or right you will walk normally, and if you double press left or right you begin to run.

I've tried to do it my self and the code won't work. I've also gone to the internet but can find any tutorials.

____This is my code____ (PS I've only been coding since November 2017 so I'm kind of a noob.)
(I'm using game maker 1.4)

//Running

if key_right
{
if (alarm[0] = -1) alarm = 15 // setting the alarm
}

if key_right and alarm[0] > -1 //checking if the alarm has not "run out"
{
mspd = mspd_run //mspd is the players default speed and mspd_run is when you are running.
}
 

Tthecreator

Your Creator!
shouldn't
Code:
if (alarm[0] = -1) alarm = 15 // setting the alarm
be
Code:
if (alarm[0] = -1) alarm[0] = 15 // setting the alarm
Your approach to the problem seems good though. Though what is your value of key_right?
I'd suggest you'd use keyboard_check_pressed and not keyboard_check.
 
W

Woochi

Guest
if key_right
{
if (alarm[0] = -1) alarm = 15 // setting the alarm
}

if key_right and alarm[0] > -1 //checking if the alarm has not "run out"
{
mspd = mspd_run //mspd is the players default speed and mspd_run is when you are running.
}
Lets simulate this, keeping in mind i just pressed key_right.
Code:
if key_right
{
if (alarm[0] = -1) alarm = 15 // setting the alarm
}
This block will then run, as the alarm is -1 initialised i assume. The alarm will be set to 15.

Directly after that, we see this
Code:
if key_right and alarm[0] > -1 //checking if the alarm has not "run out"
{
mspd = mspd_run //mspd is the players default speed and mspd_run is when you are running.
}
key_right is still true, since we are still in the same step, the key_right is still registered as pressed. It does not get reset just because we read the variable key_right a few lines before this one. The alarm was just set to 15, so both conditions obtain, and the block is executed: running speed is applied, all because of 1 key press, as you can see.

Try this instead:
Code:
If key_right {
      If alarm[0]>-1 {
            //Code for runspeed
      } else {
            //Walk code and
            Alarm[0] = 15;
      }
}

Also, as tThecreator points out, i assume key_right is true when a check_pressed() event applies, rather than check(), otherwise it returns true all the time instead of once (just that step)

PS it may not be my place, or the time, to say this but perhaps you want to start using == as comparison and = solely during assigning values. Or maybe not (if you intend to only use gamemaker, always, and the syntax remains this lenient forever.
 
Last edited by a moderator:
C

CedSharp

Guest
shouldn't
Code:
if (alarm[0] = -1) alarm = 15 // setting the alarm
be
Code:
if (alarm[0] = -1) alarm[0] = 15 // setting the alarm
Lets simulate this, keeping in mind i just pressed key_right.
Try this instead:
Code:
If key_right {
      If alarm>-1 {
            //Run code
      } else {
            //Walk code and
            Alarm = 15
      }
}
Both @Tthecreator and @Woochi are right. I can see 2 problems with your code:

1. You are assigning a variable 'alarm' without an index. I don't think this is what you wanted to do. So in theory, your code will never set the alarm.
2. Like @Woochi pointed out, keyboard key is update only once per step event. So if you do 2 different checks, the value will not have changed. In your current case, let's say the alarm works, then you will never be able to walk since the alarm is not -1 and the key is still true.
 
W

Woochi

Guest
Both @Tthecreator and @Woochi are right. I can see 2 problems with your code:

1. You are assigning a variable 'alarm' without an index. I don't think this is what you wanted to do. So in theory, your code will never set the alarm.
2. Like @Woochi pointed out, keyboard key is update only once per step event. So if you do 2 different checks, the value will not have changed. In your current case, let's say the alarm works, then you will never be able to walk since the alarm is not -1 and the key is still true.
Oh, right, i completely missed the alarm thing in his actual code :D . I shouldnt have used it as pseudocode either, will edit now
 
Top