Movement Trouble

P

PixleGamer1999

Guest
So I am having some trouble with a simple movement problem. What I want to happen is if one out of the WASD keys is pressed first while 2 are pressed simultaneously that whichever key was pressed first that is the direction you will go. For example, if I press both A and D my character can't move. I want to have it so that if I press A then also Press D I will still go Left instead of stopping.
 

YoSniper

Member
I would recommend a state machine for something like this. It might seem a bit complicated, but you can use this kind of logic for other parts of your game, too.

Create
Code:
state = "IDLE";
Step
Code:
///State management
switch(state) {
    case "IDLE":
        if keyboard_check(ord("A")) {
            state = "LEFT";
        } else if keyboard_check(ord("D")) {
            state = "RIGHT";
        } else if keyboard_check(ord("W")) {
            state = "UP";
        } else if keyboard_check(ord("S")) {
            state = "DOWN";
        } else {
            hspeed = 0;
            vspeed = 0;
        }
        break;
    case "LEFT":
        if not keyboard_check(ord("A")) {
            state = "IDLE";
        } else {
            hspeed = -2;
        }
        break;
    case "RIGHT":
        if not keyboard_check(ord("D")) {
            state = "IDLE";
        } else {
            hspeed = 2;
        }
        break;
    case "UP":
        if not keyboard_check(ord("W")) {
            state = "IDLE";
        } else {
            vspeed = -2;
        }
        break;
    case "DOWN":
        if not keyboard_check(ord("S")) {
            state = "IDLE";
        } else {
            vspeed = 2;
        }
        break;
}
 
P

PixleGamer1999

Guest
Ok, thank you for the advice I have worked with state machines before so they are not that complicated for me I just haven't used game maker in a while so I didn't think to use one thank you.
 
Top