Been bugging me for more than a year now....
I have a 2D character in a state machine I have set up with a turn state, supposed to ONLY handle the transition from turning left to right and vice versa.
The turn state checks the AnimatedSprites flip value, and either passes onto the walk state if (from Idle) its pointing in the same direction as moving to, or if pointing in the opposite direction from moving to, it plays the (3 frame) animation for the turn, and do NOTHING else until its complete, THEN passes on to the walk state.
if you hold down the button, the turn is completed successfully and passes to the walk state. But if you TAP the button, the animation doesnt complete, and it snaps back to the position it was previously looking in.
You can see what I mean from the gif here....
How the hell do I get the animation to finish playing before passing onto the next state. This snap back is annoying me so bad!
Yes yes, I get it, dont use yields use signals, but this is the only way I have got it to work. Albeit with the key tapping bug. Without these, with the button held down, it doesnt wait for the animation to finish and passes straigt onto the walk state.
extends State
onready var move: = get_parent()
# interface for the state
#func unhandled_input(event: InputEvent) -> void:
# move.unhandled_input(event)
func enter(msg: Dictionary = {}) -> void:
print("Turn State")
var flip = owner.get_node("AnimatedSprite").flip_h
# pass the message to the parent move state...
move.enter(msg)
if move.get_move_direction().x > 0.0 && flip == false:
_state_machine.transition_to("Move/Run")
if move.get_move_direction().x < 0.0 && flip == true:
_state_machine.transition_to("Move/Run")
if move.get_move_direction().x > 0.0 && flip == true:
owner.get_node("AnimationPlayer").play("Turn")
yield(owner.get_node("AnimationPlayer"), "animation_finished")
flip = false
_state_machine.transition_to("Move/Run")
if move.get_move_direction().x < 0.0 && flip == false:
owner.get_node("AnimationPlayer").play("Turn")
yield(owner.get_node("AnimationPlayer"), "animation_finished")
flip = true
_state_machine.transition_to("Move/Run")
func exit() -> void:
get_parent().exit()
Tried getting a method to fire from within the AnimationPlayer once the animtaion hits the last frame, but Im not diong that right either apparently.
Tried adding the signal to make it work, (without the above yeld and state.transition_to parts) but same problem, just snaps back as soon as the key is just tapped...
func _go_to_run_state():
_state_machine.transition_to("Move/Run")
func _on_AnimationPlayer_animation_finished(anim_name):
if anim_name == "Turn":
_go_to_run_state()
print(anim_name, " finished")
I kinda need this to work as its part of the nice smooth animation I have been going for and its going to be needed to position the character accurately sometimes.
Ideas?