Godot Version
4.2.1
Question
Hello! I’m developing a 2d platformer, and I stuck at animations. You see, when the player pressed jump, my animation switches, and stops on the 1st frame. Can anyone explain me what is happening!?
My code:
extends CharacterBody2D
@export var acceleration_speed : int = 5000
@export var max_speed : int = 150
@export var jump_force : int = 250
@export var coyote_time : float = 0.5
var is_jumping : bool = false
var can_jump : bool = true
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
@onready var anim_sprite = $AnimatedSprite2D
@onready var coyote_timer = $Coyote_timer
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
if can_jump:
coyote_timer.start(coyote_time)
#get_tree().create_timer(coyote_time).timeout.connect(coyote_timeout)
else:
can_jump = true
coyote_timer.stop()
velocity.x = clamp(velocity.x, -max_speed, max_speed)
var direction = Input.get_axis("left", "right")
velocity.x = direction * max_speed
if direction != 0:
velocity.x = move_toward(velocity.x, max_speed * direction, acceleration_speed * delta)
if velocity.x < 0:
anim_sprite.flip_h = true
elif velocity.x > 0:
anim_sprite.flip_h = false
if Input.is_action_just_pressed("jump") and can_jump:
jump()
is_jumping = true
can_jump = false
if velocity.y > 0:
is_jumping = false
#if Input.is_action_pressed("left"):
#anim_sprite.flip_h = true
#velocity.x -= acceleration_speed * delta
#if is_on_floor() and !is_jumping:
#if anim_sprite.animation != "run":
#anim_sprite.play("run")
#if Input.is_action_pressed("right") and !is_jumping:
#velocity.x += acceleration_speed * delta
#anim_sprite.flip_h = false
#if is_on_floor():
#if anim_sprite.animation != "run":
#anim_sprite.play("run")
update_animations(delta)
move_and_slide()
func jump() → void:
velocity.y = -jump_force
is_jumping = true
func update_animations(delta) → void:
if velocity.x != 0:
anim_sprite.play(“run”)
else:
anim_sprite.play(“idle”)
if velocity.y > 0 and anim_sprite.animation != "jump":
anim_sprite.play("fall")
is_jumping = false
elif is_jumping and anim_sprite.animation != "jump":
anim_sprite.play("jump")
func coyote_timeout() → void:
can_jump = false