Legacy GM How To Double Tap For Dashing?

M

MegaGamer99

Guest
Greetings, Game Makers!

I planned on adding a Dashing mechanic to my player, but have no idea how to do it. I've already put a Dashing, dashspeed, and doubletap variable in my Create event. Now, I want the player to use the dashspeed instead of movespeed when I press either the left or right twice. I also want the code to be short and simple, too. If you guys help, I would appreciate it.

BTW, here's my code for reference:

Code:
Create Event:

execute code:

//-----Intitialize Variables-----//
movespeed = 2; //Moving speed (2 pixels per frame)
flyspeed = -4;
slidespeed = 7;
sliding_friction = 0.5;
dashspeed = 4;
jumpspeed = -7; //Jumping speed (negative because it goes up)
grav = 0.6; //Gravity strength (usually a small value)

//These variables are actually used to move the player
hsp = 0; //Horizontal speed
vsp = 0; //Vertical speed
ground = 1; //Whether the player is on the ground

flying = false;
falling = false;
step = 0;
ducking = false;
sliding = false;
dashing = false;
doubletap = 2;


Step Event:

execute code:

//-----Keyboard Input-----//
Key_Left = keyboard_check(vk_left);
Key_Right = keyboard_check(vk_right);
Key_Jump = keyboard_check_pressed(ord('Z'));
//This one's for variable jump height:
Key_Fall = keyboard_check_released(ord('Z'));
Key_Attack = keyboard_check(ord('X'));
Key_Down = keyboard_check(vk_down);

//Horizontal Motion
if (Key_Left) and (!ducking) and (!sliding)
{
    hsp = -movespeed; //Walk left
    image_xscale = -1; //Face left
}
if (Key_Right) and (!ducking) and (!sliding)
{
    hsp = movespeed; //Walk right
    image_xscale = 1; //Face right
}

//Movement Key Release
if (!Key_Left) and (!Key_Right) and (!sliding) hsp = 0;

//Vertical Motion
//Is the player in the air?
if (place_meeting(x,y+1,obj_blockpar)) ground = 1;
else ground = 0;

if (Key_Jump and ground and !ducking and !sliding)
{
   vsp = jumpspeed;
}
//Variable Jumping
if (!flying)
{
if (Key_Fall and !ground and vsp < -3) vsp = -2;
}

//Fall with gravity
if (!ground) vsp += grav;
//Limit Vspeed
if (!flying)
{
    if (vsp > 6) vsp = 6;
}

if (flying)
{
    if (vsp > 1) vsp = 1;
}

//Flying
if (!ground) and (Key_Jump) and (!ducking) and (!sliding)
flying = true;

if (Key_Jump) and (flying)
vsp = flyspeed;

if (Key_Attack and flying)
{
    flying = false;
}

if (flying) faling = false;

//Falling
if (!ground and vsp > 0 and !falling and !flying)
{
    step += 1;
    }else{
    step = 0;
}

if (step = 17 and !falling and !flying)
{
    if (step < 17) step = 17;
    falling = true;
}

if (ground and falling)
{
    vsp = -4;
}

if (falling) and (!vsp)
falling = false;

//Ducking
if (Key_Down and ground and !ducking and !flying and !falling and vsp = 0)
ducking = true;

if (ducking) and (sprite_index = spr_player)
sprite_index = spr_player_duck;
if (!ducking) and (sprite_index = spr_player_duck)
sprite_index = spr_player;

if (!Key_Down) and (ducking)
ducking = false;

if (ducking) and (!ground)
ducking = false;

if (ducking) and (!sliding) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (!sliding) and (Key_Right) and (hsp > 0) hsp = 0;

//Sliding
if (ducking) and (Key_Jump) and (!sliding) and (ground)
{
    sliding = true;
    hsp = sign(image_xscale) * slidespeed;
}
if (sliding)
{
if abs(hsp) <= sliding_friction
    hsp = 0;
else
    hsp -= sign(hsp) * sliding_friction;
}
if (sliding) and (hsp = 0)
sliding = false;

//Dashing



//Horizontal Collision
if (place_meeting(x+hsp,y,obj_blockpar))
{
    while(!place_meeting(x+sign(hsp),y,obj_blockpar))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_blockpar))
{
    while(!place_meeting(x,y+sign(vsp),obj_blockpar))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;


Draw Event:

execute code:

draw_sprite_ext(sprite_index, -1, round(x), round(y), image_xscale, image_yscale, image_angle, image_blend, image_alpha);
 

jo-thijs

Member
To register a double tap, use a timer variable that is reset to like 20 hen you press in a direction (and if you're not ducking or sliding or something like that).
Decrease the timer variable every step it is not zero.

Before resetting the timer variable, check if the timer variable is still not zero.
If so, dashing is set to true.

Set dashing back to false when appropriate.

When setting hsp to -movespeed or movespeed, first check if dashing is true.
If so, set it to dashspeed instead.

There is no need for the variable doubletap.
 
M

MegaGamer99

Guest
Should I have to use a keyboard check pressed for the double tap?
 
M

MegaGamer99

Guest
I'm trying to code in the dashing action, but when I tested it, he's going in dashspeed and I didn't even double tapped. I need some specifics in entering dash mode.
Here's my code:

Code:
Horizontal Motion
if (Key_Left) and (!ducking) and (!sliding)
{
    image_xscale = -1; //Face left
    if (!dashing) hsp = -movespeed; else hsp = -dashspeed; //Walk left
    if (dash_timer > 0) dash_timer -= 1;
}
if (Key_Right) and (!ducking) and (!sliding)
{
    image_xscale = 1; //Face right
    if (!dashing) hsp = movespeed; else hsp = dashspeed; //Walk right
    if (dash_timer > 0)dash_timer -= 1;
}
if (!dashing) and (dash_timer = 0 or dash_timer < 0) dash_timer = 0;
if (!dashing) and (dash_timer > 0) and (ground) and (Key_Left or Key_Right) dashing = true;

//Movement Key Release
if (!Key_Left) and (!Key_Right) and (!sliding) hsp = 0; dash_timer = 20;
 

jo-thijs

Member
This part:
Code:
if (!dashing) and (dash_timer > 0) and (ground) and (Key_Left or Key_Right) dashing = true;
and this part:
Code:
dash_timer = 20;
has to be executed:
Code:
if (Key_Left) and (!ducking) and (!sliding)
{
    image_xscale = -1; //Face left
    if (!dashing) hsp = -movespeed; else hsp = -dashspeed; //Walk left
    // HERE
}
And this part:
Code:
if (dash_timer > 0) dash_timer -= 1;
has to be excuted outside the block.

By the way, I sent you a PM, but I guess you haven't noticed it yet.
Go to your inbox and show all.
 
M

MegaGamer99

Guest
//Horizontal Motion
if (Key_Left) and (!ducking) and (!sliding)
{
image_xscale = -1; //Face left
if (!dashing) hsp = -movespeed; else hsp = -dashspeed; //Walk left
if (!dashing) and (dash_timer > 0) and (ground) and (Key_Left or Key_Right) dashing = true;
dash_timer = 20;
}
if (dash_timer > 0) dash_timer -= 1;
if (Key_Right) and (!ducking) and (!sliding)
{
image_xscale = 1; //Face right
if (!dashing) hsp = movespeed; else hsp = dashspeed; //Walk right
if (!dashing) and (dash_timer > 0) and (ground) and (Key_Left or Key_Right) dashing = true;
dash_timer = 20;
}
if (dash_timer > 0) dash_timer -= 1;

I think I did something wrong when I added them together, Im still going in dashspeed without the doubletap. May you show me the correct way? BTW, thanks for the inbox message.
 
C

Claudiu

Guest
Hi everyone! Can someone help me to code a double tap to run? This is my variable and controls to the player....animation i have a separeted step event... i want to code double tap variable to change to run speed.

///Initialize Variables
grav =2;
hsp = 0;
vsp = 0;
facing = 1;
jumps = 0;
doubletap = 0;
walkspeed = 10;
runspeed = 30;
jumpspeed = 45;


///Control the Player

//Get the player input
key_right = keyboard_check(vk_right) || (gamepad_button_check(0,gp_padr) > 0);
key_left = -(keyboard_check(vk_left) || (-gamepad_button_check(0,gp_padl) < 0));
key_jump = keyboard_check_pressed(vk_space) || (gamepad_button_check_pressed(0,gp_face1));
key_jump_held = keyboard_check(vk_space) || (gamepad_button_check(0,gp_face1));

//Horizontal Movement
if key_right and place_free(x+walkspeed,y)
{
hsp += 1;
facing = 0;
if hsp >= walkspeed
{
hsp = walkspeed;
}
}

if -key_left and place_free(x-walkspeed,y)
{
hsp -= 1;
facing = 1;
if hsp <= -walkspeed
{
hsp = -walkspeed;
}
}

if !key_right && !-key_left
{
hsp = 0;
}

//Gravity
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_solid_16x16))
{
vsp = key_jump * -jumpspeed
}
{
vsp += grav
}
if (vsp < 0) && (!key_jump_held) vsp = max(vsp,0)

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_solid_16x16))
{
while(!place_meeting(x+sign(hsp),y,obj_solid_16x16))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_solid_16x16))
{
while(!place_meeting(x,y+sign(vsp),obj_solid_16x16))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
 
Top