Sprite animation and input | states?

I've got an always present button object which basically controls the state of the animated instance.

I couldn't get the animation to play correctly.
I'm using a spawner object to make sure to code will still run when an instance of the animation object doesn't exist.

It's supposed to be paused when the button is clickable and unpaused when the button is locked.
I think a state machine would help but I don't have any understanding of that since I'm relatively new to gms2

I drew a diagram to explain it better :)

Thank you for your help!
 

Attachments

I have some code for my button which looks like this...
Create:
GML:
enum grid_states {
    unselected,
    selected,
    selectedLock,
    unselectedLock,
}

state = grid_states.unselected;
Step:
GML:
lockcheck = obj_grid.image_index;

if (state  == grid_states.selected){ //6
    image_index = 1;
    obj_grid.image_speed = 0;
}
else if (state  == grid_states.unselected){ //2
    image_index = 0;
    obj_grid.image_speed = 0;
}
else if (state  == grid_states.selectedLock){ //4
    image_index = 1;
    obj_grid.image_speed = 1.5;
}
else if (state  == grid_states.unselectedLock){
    image_index = 0;
    obj_grid.image_speed = -1.5;
}

if state == grid_states.selectedLock{ //5
    if lockcheck == 87{
    state = grid_states.selected;
    }
}
if state == grid_states.unselectedLock{
    if lockcheck == 0{
    state = grid_states.unselected;
    }
}
Left Released:
GML:
if state == grid_states.unselected{ //3
    state = grid_states.selectedLock;
    exit
}
if state == grid_states.selected{
    state = grid_states.unselectedLock;
    exit
}
 

samspade

Member
Your code seems mostly good, but the code in your step event will always trigger in a given state, not merely when that state is switched to, so you will be resetting the image index every frame. You probably want to move that code to whatever triggers the state change so it only happens on a state change, not every frame.
 
Top