extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@ onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# 添加重力
if not is_on_floor():
velocity += get_floor_check_enabled() ? PhysicsServer.get_instance_singleton().get_space().get.gravity_direction() * gravity : Vector2(0, gravity) * delta
# 处理跳跃
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# 设置输入方向:-1,0,1
var direction := Input.get_axis("move_left", "move_right")
# 翻转精灵
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# 播放动画
if is_on_floor():
if direction == 0:
animated_sprite.play("player")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
# 应用运动
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
评论 (0)