如何使用 Godot 中的动画播放器跳到动画中的某个位置?

How do I skip to a position in the animation with an animationplayer in Godot?

我正在用 godot 制作游戏并且必须存储动画位置,因为动画被另一个动画暂停。我想继续之前的动画,这就是为什么我必须存储动画位置然后将其设置为存储值的原因。

我试过设置它(没用),但在文档和互联网上的其他地方我没有发现任何有用的东西。

这是上面的脚本:

extends KinematicBody2D

onready var animation_player = $AnimationPlayer

var hurt_anim_playing: bool = false
var hurt_anim_progress: float = 0

func _ready():
    animation_player.play("idle")
    pass

func _physics_process(delta): 
    # for each touching heart, get hurt
    for body in hitbox.get_overlapping_bodies(): 
        if body.has_method("heart"):
            G.health -= 1
            hurt_anim_playing = true
            hurt_anim_progress = animation_player.current_animation_position
            animation_player.play("hurt")
            update_sprite()
            body.queue_free()


func die():
    dieLayer.visible = true 
    get_tree().paused = true

func update_sprite():
    sprite.frame = G.max_health - G.health 
    if G.health == 0:
        die()


func _on_AnimationPlayer_animation_finished(anim_name):
    if anim_name == "hurt":
        hurt_anim_playing = false 
        animation_player.play("idle")
        animation_player.current_animation_position = hurt_anim_progress


其实我想设置动画的位置,让动画从停止的地方继续播放,结果报错

问题是 current_animation_position 只有 getter,没有 setter: https://docs.godotengine.org/en/3.1/classes/class_animationplayer.html#class-animationplayer-property-current-animation-position

要将动画设置到特定点,可以使用 advance(float delta),如果你想处理开始点和恢复点之间的所有内容,或者使用 seek(float seconds, bool update=false),如果你只是想跳到新位置。如果需要将 update 设置为 true,您可以测试一下。文档说 "If update is true, the animation updates too, otherwise it updates at process time."

您的代码将如下所示:

func _physics_process(delta): 
# for each touching heart, get hurt
for body in hitbox.get_overlapping_bodies(): 
    if body.has_method("heart"):
        G.health -= 1
        hurt_anim_playing = true
        hurt_anim_progress = animation_player.current_animation_position
        animation_player.play("hurt")
        update_sprite()
        body.queue_free()


func _on_AnimationPlayer_animation_finished(anim_name):
    if anim_name == "hurt":
        hurt_anim_playing = false 
        animation_player.play("idle")
        animation_player.seek(hurt_anim_progress) #maybe (hurt_anim_progress, true)