Hi.
As for the title, i’m having issues with handling an AnimatedSprite2D, I have absolutely no idea how to properly handle the way animations work through a script.
I’m using the template CharacterBody2D and I modified it this way:
extends CharacterBody2D
@onready var character_sprite: AnimatedSprite2D = $character_sprite
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
character_sprite.animation = "jump"
if is_on_floor():
character_sprite.animation = "idle"
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
character_sprite.animation = "run"
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
var isLeft = velocity.x < 0
character_sprite.flip_h = isLeft
move_and_slide()
I’m pretty sure this is VERY WRONG as I end up with the sprite disappearing or freezing completely.
Any help?
I noticed one thing specifically from the code, which is that your run animation will only play in the frame the key is first pressed down, and on the next frame will immediately be reverted back to the idle animation.
Other than that, is the sprite sheet for the animation set up correctly? One reason the sprite might be disappearing is that some of the frames are taken from empty areas in the sprite sheet.
Be honest code is messy , do you mind to explain what do you expect character to do ?
AnimationPlayer2d is great to use for spites and skeletons instead , but with this negative gravity and floor check and toward I’m not sure what you want achieve .
Maybe I’ll end up sending here a video of the issue at this point
I’m pretty sure the issue with my logic contraddicting itself
I may have found an issue myself
gonna check soon.
I fixed rechecking code from an old prototype of the project and keeping a key detail from it.
I decided for the character to “air walk” when jumping as the jumping animation itself isn’t made well from the pack I purchased.
I think you shouldn’t play the animation ‘run’ when the character is supposed to jump up? Plus, you’ve got another piece of code both making the player jump up, but for one of the animation you play ‘run’ and for the other one ‘jump’. Delete this piece of code:
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY
character_sprite.animation = “run”
and replace it with this:
if Input.is_action_just_pressed(“walk”): #in the input settings make “walk” the left and right arrow keys
character_sprite.play(“run”)
then again, swap this piece of code:
if not is_on_floor():
velocity += get_gravity() * delta
character_sprite.animation = “jump”
if is_on_floor():
character_sprite.animation = "idle"
for this piece of code:
if is_on_floor() and Input.is_action_just_pressed(“ui_accept”):
velocity += get_gravity() * delta
character_sprite.animation = “jump”
elif not Input.is_action_just_pressed(“walk”):
character_sprite.animation = “idle”