如何在Godot中使用animationplayer跳到动画中的某个位置?

时间:2019-05-31 19:03:35

标签: animation godot

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

我尝试设置它(没有用),在文档和Internet上的其他地方,我发现没有任何帮助。

这是上面的脚本:

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


实际上,我想设置动画的位置,并让动画在停止处继续播放,但是却出现了错误

1 个答案:

答案 0 :(得分:0)

问题在于current_animation_position只有一个吸气剂,而没有一个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,则可以进行测试。文档说:“如果更新为true,则动画也会更新,否则会在处理时更新。”

您的代码将看起来像这样:

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)