Channging animations with an Animation Sprite

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

I’m trying to assign different animations to movements of a Player character. However, I’m facing some sort of issue in my code: writting “else: _animated sprite.stop()” to every individual line appears to cancel the animations all together, whilst writting it in single line allows only one of the animations to play.

extends KinematicBody2D

export (int) var speed = 200

onready var _animated_sprite = $AnimatedSprite

var velocity = Vector2()

func get_input():
velocity = Vector2()

if Input.is_action_pressed("ui_right"):
	velocity.x += 1
	_animated_sprite.play("Right")
else:
	_animated_sprite.stop()
	
if Input.is_action_pressed("ui_left"):
	velocity.x -= 1
	_animated_sprite.play("Left")
else:
	_animated_sprite.stop()

if Input.is_action_pressed("ui_down"):
	velocity.y += 1
	_animated_sprite.play("Forward")
else:
	_animated_sprite.stop()

if Input.is_action_pressed("ui_up"):
	velocity.y -= 1
velocity = velocity.normalized() * speed
:bust_in_silhouette: Reply From: ipdramon

You should consider using an AnimationTree instead.
With the BlendSpace2D Godot itself will change the animation based on your input.

This part in the tutorial specifically shows how to use the BlendSpace2D.
And this youtube video should show the steps to get an AnimationTree to do the heavy lifting in a 2D game: https://www.youtube.com/watch?v=Xf2RduncoNU

Good luck with your project!

Thank you for the help!

Herzvier | 2023-01-22 09:26