Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | TonyRedgrave |
I don’t know why this error appears:
Invalid get index ‘animation’ (on base: ‘AnimationPlayer’).
I am doing my first project on godot
extends KinematicBody2D
onready var anim: AnimationPlayer = get_node("anim")
var velocity: Vector2
export(int) var speed = 200
var facingDir = Vector2()
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
facingDir = Vector2(1,0)
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
facingDir = Vector2(-1,0)
if Input.is_action_pressed("ui_down"):
velocity.y += 1
facingDir = Vector2(0,1)
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
facingDir = Vector2(0,-1)
velocity = velocity.normalized() * speed
func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
if velocity.x > 0:
play_animation("right")
elif velocity.x < 0:
play_animation("left")
elif velocity.y > 0:
play_animation("up")
elif velocity.y < 0:
play_animation("low")
elif facingDir.x == 1:
play_animation("idle_right")
elif facingDir.x == -1:
play_animation("idle_left")
elif facingDir.y == 1:
play_animation("idle_low")
elif facingDir.y == -1:
play_animation("idle_up")
func play_animation(anim_name):
if anim.animation != anim_name:
anim.play(anim_name)