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

GameMaker My state machine changes state with no input

K

keex

Guest

switch(dirr_state){
case direction_state.left:
x_sprite_center = x + (sprite_width/2);
y_sprite_center = y + (sprite_height/2);
if(xx >= x_sprite_center){
dirr_state = direction_state.right;
image_xscale = -1;
}
break;
case direction_state.right:
x_sprite_center = x - (sprite_width/2);
y_sprite_center = y + (sprite_height/2);
if(xx <= x_sprite_center){
dirr_state = direction_state.left;
image_xscale = 1;
}
break;
}



This is the code and the state machine works as long as you hold down the button but as soon as you let go it moves to the position and then changes the state back to left. Been trying to figure out why this is happening but cant find a solution. if i were to add the image_xscale variable before the if statements it kinda works but i get some visual bugs where it reverts back and forth 1 time before settling on the state.
 
Here, if(xx >= x_sprite_center){, my guess would be that xx == x_sprite_center, which would mean that that line of code triggers the dirr_state = direction_state.right in the first code block, then xx still == x_sprite_center, so the next step, the if(xx <= x_sprite_center){ triggers in the second code block, meaning that dirr_state = direction_state.left gets triggered as well. It might alternate back and forth or it might get stuck on one depending on exactly what is happening. But I can say that seems like a bizarre way to set up a state machine? Why not have it change based on key presses?
 
K

keex

Guest
Its set up on mouse click if i press to the left of the sprite i want it to turn left and vise versa.
Im not fully understandning what you mean, but it all works as long as the mouse key is down or while the character is still moveing to the target location. It happens when it gets there it just changes the state and reverts back
 
K

keex

Guest
Im guessing that the sprite gets past the target location and the mouseclick is still registered and it reverts back
 
So the character moves to xx? Once it gets there, it'll -always- trigger this code:
Code:
if(xx <= x_sprite_center){
           dirr_state = direction_state.left;
           image_xscale = 1;
       }
Because x_sprite_center will end up equaling xx.
 
K

keex

Guest
I tried removing the '=' sign and getting the same results. Also tried adding a buffer but that only fixed the visual bug if you clicked in the center of the sprite where it would alternate between them.
 
K

keex

Guest
SOLVED!

I added another condition to the if statement that the state can only change when the mouse button is pressed and that solved it.
 
Top