• 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!

Stopping diagonal moving

Hello! I'm working on an rpg and I don't know how to stop the diagonal movement.
I use this code link:

///GET INPUTS
up = keyboard_check(ord("W"));
down = keyboard_check(ord("S"));
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));

///INT MOVEMENT
hMove = (right-left) * spd;
vMove = (down - up) * spd;

///HORIZONTAL COLLISION
if(place_meeting( x + hMove, y, oSolid))
{
while(!place_meeting(x + sign(hMove),y,oSolid))
x += sign(hMove);
hMove = 0;
}

///VERTICAL COLLISION
if(place_meeting( x, y + vMove , oSolid))
{
while(!place_meeting(x ,y+sign(vMove),oSolid))
y += sign(vMove);
vMove = 0;
}

///MOVE
x += hMove;
y += vMove;






What can I do to move the player only on 4 ways?
 

Nidoking

Member
Well, you're handling horizontal and vertical movement independently. You'd need to make it do only one or the other, not both.
 

4i4in

Member
One way is switch statement with break (ignore ds_list operations - You are intrested only in x/y):
GML:
        switch (keyboard_key)
        {
        case ord("A"):
            ds_list_set(char_list,3,xxx1 -1);io_clear();    break;
        case ord("D"):
            ds_list_set(char_list,3,xxx1 +1);io_clear();    break;
        case ord("W"):
            ds_list_set(char_list,4,yyy1 -1);io_clear();    break;
        case ord("S"):
            ds_list_set(char_list,4,yyy1 +1);io_clear();    break;   
        }
but if You are about to allow many inputs (keyboard, pad buttons, pad sticks) You can use value that is check to forbid diagonal:
zzz = 0; //move done - forbiden diagonal move using diferent controls like keyboard/gamepad buttons/stick

and executed in loop:
Code:
    //gamepad direction buttons
    if gamepad_button_value(gmpd_pl1, gmpd_pl1_pb2) + gamepad_button_value(gmpd_pl1, gmpd_pl1_pb3) > 0
        {   
            ds_list_set(char_list,3,xxx1 - gamepad_button_value(gmpd_pl1, gmpd_pl1_pb2) + gamepad_button_value(gmpd_pl1, gmpd_pl1_pb3)    );   
            zzz = 1;
        }
    else if gamepad_button_value(gmpd_pl1, gmpd_pl1_pb0) + gamepad_button_value(gmpd_pl1, gmpd_pl1_pb1) > 0
        {   
            ds_list_set(char_list,4,yyy1 - gamepad_button_value(gmpd_pl1, gmpd_pl1_pb0) + gamepad_button_value(gmpd_pl1, gmpd_pl1_pb1)    );
            zzz = 1;
        }


    //xaxis dominate
    if abs(xxx0) > abs(yyy0)    && zzz = 0
    {    //move x
        ds_list_set(char_list,3,xxx1 + ceil(xxx0));
        zzz = 1;
    }
    //yaxis dominate
    if abs(xxx0) < abs(yyy0)    && zzz = 0
    {    //move y
        ds_list_set(char_list,4,yyy1 - ceil(yyy0));
        zzz = 1;
    }

    if zzz = 0
    {
        switch (keyboard_key) //...case above for You
 
Top