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:
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);