T
toothmosnter
Guest
I have a state machine and the "playerStateHide" has some issue with executing that I can't seem to figure out. I know it has to do with calling playerStateFree at the end of the hide function, as when I remove that line of code and mapped playerStateFree to a keybind, the glitch did not occur. I've also tried setting speed = 0 (and all my movement variables, inputMagnitude = 0, hSpeed = 0, vSpeed = 0, etc...) before the playerStateFree is called, but that causes this to happen instead:
My code is below. Any help would be greatly appreciated!
State machine script:
GML:
// ----------------------------FREE STATE
function playerStateFree(){
//Movement
hSpeed = lengthdir_x(inputMagnitude * speedWalk,inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk,inputDirection);
playerCol();
//Update Sprite Index
var _oldSprite = sprite_index;
if (inputMagnitude !=0)
{
direction = inputDirection;
sprite_index = spriteWalk;
} else sprite_index = spriteIdle;
if (_oldSprite != sprite_index) localFrame = 0;
AnimateSprite();
ChangePlayerState();
}
// ----------------------------HIDE STATE
function playerStateHide(){
hSpeed = lengthdir_x(inputMagnitude * speedHide,inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedHide,inputDirection);
playerCol();
//Update Sprite Index
var _oldSprite = sprite_index;
sprite_index = spriteHide;
if (_oldSprite != sprite_index) localFrame = 0;
AnimateSprite();
if (!keyHide) state = playerStateFree;
}
// ----------------------------CHANGE STATE
function ChangePlayerState() {
if (keyHide)
{
state = playerStateHide;
}
}
GML:
keyLeft = keyboard_check(vk_left) || keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) || keyboard_check(ord("D"));
keyUp = keyboard_check(vk_up) || keyboard_check(ord("W"));
keyDown = keyboard_check(vk_down) || keyboard_check(ord("S"));
keyDash = keyboard_check_pressed(vk_shift) || keyboard_check_pressed(ord("Z"));
keyHide = keyboard_check(vk_control) || keyboard_check(vk_lcommand) || keyboard_check(vk_rcommand) || keyboard_check(ord("X"));
keyActivate = keyboard_check_pressed(vk_space) || keyboard_check(ord("E"));
inputDirection = point_direction(0,0, keyRight - keyLeft, keyDown - keyUp);
inputMagnitude = (keyRight - keyLeft != 0) || (keyDown - keyUp != 0);
if (!global.gamePaused) script_execute(state);