- Edited
Ok, I have been trying to get this to work for about 3 or 4 weeks of spare time and CANNOT get it to do what I want....
I have a short (2 frame) animation for a character to turn, I managed to get it working PERFECTLY the way I wanted with this code using a standard Input process.....
func applyMovement(delta):
if Input.is_action_pressed("ui_left"):
if $AnimatedSprite.flip_h == false:
print("F")
$AnimatedSprite.play("Turn")
yield($AnimatedSprite, "animation_finished")
velocity.x = 0
$AnimatedSprite.flip_h = true
direction = -1
speed.x = MAX_SPEED * direction
$AnimatedSprite.play("Walk")
elif Input.is_action_pressed("ui_right"):
if $AnimatedSprite.flip_h == true:
print("Q")
$AnimatedSprite.play("Turn")
yield($AnimatedSprite, "animation_finished")
velocity.x = 0
$AnimatedSprite.flip_h = false
direction = 1
speed.x = MAX_SPEED * direction
$AnimatedSprite.play("Walk")
However, I am trying to impliment this using a finite state machine, (using the GDQuest pro 2d game course version) and this doesnt work anymore! I dont know why....
I have things broken down into idle, turn and move states, and I want to have the 2 frame animation complete before passing on to the move state, but yield doesnt seem to work anymore the way it did in the above code....
It doesnt matter where I put any of the instructions, it just deosnt work. I have changed it so many times, put it in so many places, I dont know what do to now.
Heres the code for the 'turn state'
extends 'state.gd'
func enter(host):
pass
func handle_input(host, event):
if event.is_action_pressed('jump'):
return 'jump'
func update(host, delta):
var input_direction = get_input_direction()
if input_direction.x == -1:
if host.get_node('AnimatedSprite').flip_h == true:
return 'move'
elif host.get_node('AnimatedSprite').flip_h == false:
turn(host)
if input_direction.x == 1:
if host.get_node('AnimatedSprite').flip_h == false:
return 'move'
elif host.get_node('AnimatedSprite').flip_h == true:
turn(host)
func get_input_direction():
var input_direction = Vector2()
input_direction.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
return input_direction
func turn(host):
var input_direction = get_input_direction() # get the input direction
if input_direction.x == -1: # left
if host.get_node('AnimatedSprite').flip_h == false:
print("Turn LEFT")
print(host.look_direction)
host.get_node('AnimatedSprite').play("turn")
host.get_node('AnimatedSprite').flip_h = true
print("Turned LEFT")
yield(host.get_node('AnimatedSprite'), "animation_finished")
print("Anim finish L")
return 'move'
elif input_direction.x == 1: # right
if host.get_node('AnimatedSprite').flip_h == true:
print("Turn RIGHT")
print(host.look_direction)
host.get_node('AnimatedSprite').play("turn")
host.get_node('AnimatedSprite').flip_h = false
print("Turned RIGHT")
yield(host.get_node('AnimatedSprite'), "animation_finished")
print("Anim finish R")
return 'move'
Now, although it prints through all the Print statements in the output, so it IS trawling through the code as intended, its not stopping (yielding) at the turn animation anymore. The 1st frame of the turn animation is played super brifely, before moving on to the 'move' state. HELP! ( and I dont know how to get all the code to format properly, sorry for hard reading)