GameMaker How to do Sudden Movement direction Change?

BeowuIf

Member
Hello Everyone my name is Beowulf and i'm making my first platform game on GMS2

I would like to know how can i program an antecipation turn frame/s before the player suddenly change the walking movement direction



i know that the super mario bros games had this mechanic as well
upload_2019-10-20_11-3-0.png

thanks for reading this
Beowulf
 

Simon Gust

Member
An idea would be to implement acceleration and deceleration, then when you decelerate from one direction while holding in the other direction it plays this animation.
 

BeowuIf

Member
hey @Simon Gust any tips on how i can implement accel and decel ?

this is my player movement code


Code:
#region
// Capturar input do jogador
if(hascontrol)
{
    key_left = keyboard_check(vk_left) || keyboard_check(ord("A"));
    key_right = keyboard_check(vk_right)|| keyboard_check(ord("D"));
    key_down = keyboard_check(vk_down) || keyboard_check(ord("S"));
    key_up = keyboard_check(vk_up) || keyboard_check(ord("W"));
    key_jump = keyboard_check_pressed(vk_space);

    if (key_left) || (key_right) || (key_jump) || (key_up) || (key_down)
    {
        controller =0;
    }

    if (abs(gamepad_axis_value(0,gp_axislh)) > 0.2)
    {
        key_left = abs(min(gamepad_axis_value(0,gp_axislh),0));
        key_right = max(gamepad_axis_value(0,gp_axislh),0);
        controller =1;
    }

    if (gamepad_button_check_pressed(0,gp_face1))
    {
        key_jump =1;
        controller =1;   
    }
}
else
{
    key_right =0;   
    key_left =0;
    key_jump =0;
}
#endregion

#region
/// calculo do movimento
var move = key_right - key_left

hsp = move * walksp;

vsp = vsp + grv;

canjump -=1;
if (canjump>0)  && (key_jump)
{
    vsp    = -8;
    canjump = 0;
}

/// colisão horizontal
if place_meeting(x+hsp,y,obj_block)
{
    while (!place_meeting(x + sign(hsp),y,obj_block))
    {
        x = x + sign(hsp);
    }
    hsp = 0;   
}

x = x + hsp;

/// colisão horizontal
if place_meeting(x,y+vsp,obj_block)
{
    while (!place_meeting(x,y+ sign(vsp),obj_block))
    {
        y = y + sign(vsp);
    }
    vsp = 0;   
}

y = y + vsp;

#endregion

#region //Animações
if(!place_meeting(x,y+1,obj_block))
{
    image_speed = 3;
    
    if(sign(vsp)==1)
    {//Falling
        sprite_index = spr_hiro_jump_03
    }
    else if (global.hasgun == false)
    { //Rising
        sprite_index = spr_hiro_jump_02
    }
    else
    {
        sprite_index = spr_hiro_jump_gun_02
    }
    
}

else
{

    
    canjump = 15;
    if(sprite_index == spr_hiro_jump_03)
    {
        repeat(5)
        {
            with (instance_create_layer(x,bbox_bottom,"Bullets",obj_poeira))
            {
                vsp = 0;   
            }
        }       
    }
    image_speed=1;
        
    
    if(hsp == 0 && global.hasgun == false )
    {
        sprite_index = spr_hiro_idle;
        
    }
    else if(hsp == 0 && global.hasgun == true )
    {
        sprite_index = spr_hiro_idle_gun;
        
    }
    else
    {
        sprite_index = spr_hiro_walking;   
    }
    

}
#endregion
 

Simon Gust

Member
I use this. It will also handle overspeed.
Code:
//Creating starting values
var acc = 0.15 * walksp; // acceleration
var dec = 0.15 * walksp; // deceleration

if (move != 0) // if you are pressing a direction
{
   // reduce overspeed via deceleration to walksp
   if (abs(hsp) > walksp)
   {
       if (hsp >  walksp) hsp = max(hsp - dec,  walksp);
       else
       if (hsp < -walksp) hsp = min(hsp + dec, -walksp);
   }
   else
   {
       // normal acceleration
       if (move > 0) hsp = min(hsp + acc,  walksp);
       else
       if (move < 0) hsp = max(hsp - acc, -walksp);
   }
}
else // not moving
{
   // reduce overspeed via deceleration to 0
   if (hsp < 0) hsp = min(hsp + dec, 0);
   else
   if (hsp > 0) hsp = max(hsp - dec, 0);
}
 
Top