Running Animation continues to play during Character Idle

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LotusOrb

I was using GameEndeavors statemachine tutorial and after some hiccups I had got it to work. However my next issue was that my character was not properly transitioning from the running state to the idle state. The running animation continued to play as the character was standing still.

Here is a look at all the code that I had used.

PlayerController.gd

extends KinematicBody2D

const UP = Vector2(0, -1)
const SLOPE_STOP = 64

var velocity = Vector2()
var move_speed = 7 * 16
var gravity = 1000
var jump_velocity = -420

onready var anim_player = $AnimationPlayer
onready var state_machine = $AnimationTree.get(“parameters/playback”)

func _apply_gravity(delta):
velocity.y += gravity * delta
print(velocity.x)

func _apply_movement():
var snap = Vector2.DOWN * 32 if !is_on_floor() else Vector2.ZERO
velocity = move_and_slide_with_snap(velocity, snap, Vector2.UP)

func _handle_move_input():
var move_direction = -int(Input.is_action_pressed(“left”)) + int(Input.is_action_pressed(“right”))
velocity.x = lerp(velocity.x, move_speed * move_direction, _air_res())
if move_direction!=0:
$AnimatedSprite.scale.x = move_direction

func _air_res():
return 0.2 if is_on_floor() else 0.1

func dead():
pass


StateMachine.gd

extends Node
class_name StateMachine

var state = null setget set_state
var previous_state = null
var states = {}

onready var parent = get_parent()

func _physics_process(delta):
if state != null:
_state_logic(delta)
var transition = _get_transition(delta)
if transition != null:
set_state(transition)

func _state_logic(delta):
pass

func _get_transition(delta):
return null

func _enter_state(new_state, old_state):
pass

func _exit_state(old_state, new_state):
pass

func set_state(new_state):
previous_state = state
state = new_state

if previous_state != null:
	_exit_state(previous_state, new_state)
if new_state != null:
	_enter_state(new_state, previous_state)

func add_state(state_name):

states[state_name] = states.size()

PlayerFSM.gd

extends StateMachine

func _ready():
add_state(“idle”)
add_state(“run”)
add_state(“jump”)
add_state(“fall”)
call_deferred(“set_state”, states.idle)

func _input(event):
if [states.idle, states.run].has(state):
if event.is_action_pressed(“jump”):
parent.velocity.y = parent.jump_velocity

func _state_logic(delta):
parent._handle_move_input()
parent._apply_gravity(delta)
parent._apply_movement()

func _get_transition(delta):
match state:
states.idle:
if !parent.is_on_floor():
if parent.velocity.y < 0:
return states.jump
elif parent.velocity.y > 0:
return states.fall
elif parent.velocity.x !=0:
return states.run
states.run:
if !parent.is_on_floor():
if parent.velocity.y < 0:
return states.jump
elif parent.velocity.y > 0:
return states.fall
elif parent.velocity.x == 0:
return states.idle
states.jump:
if parent.is_on_floor():
return states.idle
elif parent.velocity.y >= 0:
return states.fall
states.fall:
if parent.is_on_floor():
return states.idle
elif parent.velocity.y < 0:
return states.jump

return null

func _enter_state(new_state, old_state):
match new_state:
states.idle:
parent.anim_player.play(“Idle”)
states.run:
parent.anim_player.play(“Run”)
states.jump:
parent.anim_player.play(“Jump”)
states.fall:
parent.anim_player.play(“Fall”)

func _exit_state(old_state, new_state):
pass

Any and all help appreciated. Thanks in advance.