Godot Version
4.3
Question
This is my kill_zone function. When I try to play the player animation when the player enters the killzone, it doesn’t work. However, flip_v, flip_h, and all other methods for the AnimatedSprite2D works. I am at a loss as to why.
The Bug commented line doesn’t work but everything does
kill_zone.gd
extends Area2D
@onready var timer = $Timer
@onready var label: Label = $Label
func _on_body_entered(body):
#print("Player entered the kill zone boundary")
Engine.time_scale = 0.5
#body.get_node("CollisionShape2D").queue_free()
#Debug
#print(body.get_node("AnimatedSprite2D").is_playing())
#body.get_node("AnimatedSprite2D").stop()
#print(body.get_node("AnimatedSprite2D").is_playing())
# Bug: Unable to play animation through killzone however other function call works
# Maybe this has some answers: https://forum.godotengine.org/t/animation-player-not-working-how-fix/6780
body.get_node("AnimatedSprite2D").play("hit")
body.animated_sprite.play("hit")
# Tests: These all work but the above doesn't
#body.get_node("AnimatedSprite2D").flip_v = true
#body.animated_sprite.flip_h = true
#print(body.animated_sprite.get_playing_speed())
timer.start()
label.visible = true #Currently bound to killzone object so it can appear where the actual killzone is
func _on_timer_timeout():
Engine.time_scale = 1
get_tree().reload_current_scene()
player.gd
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction: -1, 0 or 1
var direction = Input.get_axis("move_left", "move_right")
#Flip Sprite
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# Play animations
if is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
#Apply movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()