I made some edits to your code. I think the code below should work, in theory at least. I placed comments on the parts I changed.
extends Node2D
const PLAYER_WALK_SPEED = 180
const PLAYER_RUN_SPEED = 200
const RIGHT = Vector2(1, 0)
const LEFT = Vector2(-1, 0)
var player_speed = PLAYER_WALK_SPEED
var player_direction = Vector2()
var can_move = true
var is_moving = false
var is_act = false
# We need a way to know when an animation is playing or not
var is_animation_playing = false
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
is_moving = Input.is_action_pressed("move_right") or Input.is_action_pressed("move_left")
is_act = Input.is_action_pressed("kick")
var animatedSprite = get_node("AnimatedSprite")
# Update is_animation_playing
is_animation_playing = animatedSprite.is_playing()
if (is_moving):
player_speed = PLAYER_WALK_SPEED
player_direction = Vector2()
# You had move_left on a separate if check and an else that sets speed to zero
# but your right movement was in a separate if check. I switched the move_left if
# check to a elif check. (A minor nitpick, feel free to ignore! :) )
if(can_move and Input.is_action_pressed("move_right")):
player_direction = RIGHT
elif(can_move and Input.is_action_pressed("move_left")):
player_direction = LEFT
else:
player_speed = 0
var movment = player_speed * player_direction.normalized() * delta
move_player(movment)
anim_player()
func move_player(movment):
move(movment)
func anim_player():
var animatedSprite = get_node("AnimatedSprite")
# Are we playing a animation? If we are, then return so none of the code below is called.
# NOTE: you could place this check before every animatedSprite.play if you wanted to.
if (is_animation_playing == false):
return;
if(is_act):
if(player_direction == RIGHT):
animatedSprite.play("kick_right")
else:
animatedSprite.play("kick_left")
elif(is_moving):
if(player_direction == RIGHT):
animatedSprite.play("walk_right")
else:
animatedSprite.play("walk_left")
else:
idle_player()
func idle_player():
var animatedSprite = get_node("AnimatedSprite")
if(player_direction == RIGHT):
animatedSprite.play("stop_right")
else:
animatedSprite.play("stop_left")
# This function isn't really needed, since you're changing animations in the
# anim_player function every _fixed_process call.
# (Once again, a minor nitpick on my part. Feel free to ignore :) )
func _on_AnimatedSprite_finished():
if(not is_moving):
idle_player()
Hopefully the changes work! :smile: