GMS 2.3+ character moving but not animating when using dpad or keyboard

pYo

Member
Hey everyone,

I've never used the movement outside of the analog stick before, so assigning it to the dpad is new to me, and the way my code is set up. My bet is that its to do with the lngth variable, but not sure how to fix it still. Been trying for a while and coming up short.

Here's the code, would appreciate all the help I can get, cheers.
By the way, how do I create a code block below? Thanks!

GML:
// create event
getInput(); // all the shortcuts to the controller and keyboard, such as keyRight instead of keyboard_check_right, and so on
look = 0;
spd = 3;
walkSpd = spd;
hSpd = 0;
vSpd = 0;
xAxis = 0;
yAxis = 0;
lngth = 0;
drctn = 0;
diagSpd = round(spd * (sqrt(2) / 2));
image_speed = 1;
stand = RIGHT;
face = RIGHT;
state = movement;

//step event

function movement()
{
    getInput();
    depth = -y;

    if (xAxis or yAxis) >=  gamepad_set_axis_deadzone(0, 0.27)
    {
        // get direction via analog
        drctn = point_direction(0, 0, xAxis, yAxis);

        // get the hspeed and vspeed
        hSpd = lengthdir_x(lngth, drctn);
        vSpd = lengthdir_y(lngth, drctn);

        // move
        x += hSpd;
        y += vSpd;

        // control the sprite
        image_speed = sign(lngth) * 0.6;
        if lngth == 0
        {
            image_speed = 0;
        }
        else
        {
            image_speed = 1;
        }

        // get the length
        if xAxis == 0 and yAxis == 0
        {
            lngth = 0;
        }
        else
        {
            lngth = spd;
        }
    }

    // dPad controls needs editing
    if dPadDown { vspeed = spd; sprite_index = sprGreenRunDown; } // none of the sprites animate when using the dpad
    if dPadUp { vspeed = -spd; sprite_index = sprGreenRunUp; }
    if dPadRight { hspeed = spd;  sprite_index = sprGreenRunRight; }
    if dPadLeft { hspeed = -spd;  sprite_index = sprGreenRunLeft; }

    if dPadUp && dPadLeft { sprite_index = sprGreenRunUpLeft;  }
    if dPadUp && dPadRight { sprite_index = sprGreenRunUpRight;  }
    if dPadDown && dPadLeft { sprite_index = sprGreenRunDownLeft;  }
    if dPadDown && dPadRight { sprite_index = sprGreenRunDownRight; }

    if !dPadUp && !dPadDown { vspeed = 0; }
    if  !dPadRight && !dPadLeft { hspeed = 0; }

    if (vspeed != 0) && (hspeed != 0) { spd = diagSpd; }
    else { spd = walkSpd; }
}
 
Last edited:

TheouAegis

Member
Why did you completely change your mechanics? You don't set lngth with your dpad, so of course it won't animate. Tie your dpad in with your normal controls.
 

pYo

Member
when i set lngth with it, i can only move it horizontally. i'm sadly not a smart person.
 

pYo

Member
I doesn't work properly and for some reason disables the analog movement entirely.

This is the proper placement, right:

GML:
// dPad controls needs editing
    if dPadDown { vspeed = spd; sprite_index = sprGreenRunUp;  }
    if dPadUp { vspeed = -spd; sprite_index = sprGreenRunUp;  }
    if dPadRight { hspeed = spd;  sprite_index = sprGreenRunRight; }
    if dPadLeft { hspeed = -spd;  sprite_index = sprGreenRunLeft; }

    if dPadUp && dPadLeft { sprite_index = sprGreenRunUpLeft;}
    if dPadUp && dPadRight { sprite_index = sprGreenRunUpRight;  }
    if dPadDown && dPadLeft { sprite_index = sprGreenRunDownLeft; }
    if dPadDown && dPadRight { sprite_index = sprGreenRunDownRight; }

    if !dPadUp && !dPadDown { vspeed = 0; }
    if  !dPadRight && !dPadLeft { hspeed = 0; }

    if (vspeed != 0) && (hspeed != 0) { spd = diagSpd; }
    else { spd = walkSpd; }
    
    if (vspeed == 0 || hspeed == 0) { lngth = 0 }
    else { lngth = spd; }
As always, thanks!
 

TheouAegis

Member
What I mean is why aren't you incorporating in the d-pad into xAxis, yAxis, hSpd, and vSpd? Do you want pressing up on the d-pad and pressing up on the axis to make thick Player move at TWIcE the speed? Because that's currently what is going to happen with how your code is written. that's fine if that's what you want, and which case you need to calculate lngth for BOtH movement methods at the bottom. If hspeed or vspeed aren't 0 and if hSpd or vSpd aren't 0.
 
Last edited:

pYo

Member
I get what you're saying, but I'm fairly new to controller input and am not the most experienced, so I've tried all day but didn't get anywhere with that. However I did fix it in a way that I found to be very satisfying for me.

I just give the option to toggle dPad controls v Analog controls, so nothing clashes ever, and no player will control both simultaneously so I feel safe there. In the future I might even have controls remap based on which option is chosen.

That said, I'm currently trying to write a function for the first time and failing to get the required result, so I'll open another topic and would really like to see what you think is the reason behind my issue. I'll add the link once I post the issue and the code.

Thanks Theou, appreciate it!

Edit: Here you go https://forum.yoyogames.com/index.p...for-the-first-time-and-having-an-issue.91592/
 
Top