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

如何使用Godot中的animationplayer跳到动画中的某个位置?,animation,godot,Animation,Godot,我正在戈多制作一个游戏,必须存储动画位置,因为动画被另一个暂停。我想继续之前的动画,这就是为什么我必须存储动画位置,然后将其设置为存储值 我试过设置它(不起作用),在文档和互联网上的其他地方,我没有发现任何有用的东西 这是上面的脚本: extends KinematicBody2D onready var animation_player = $AnimationPlayer var hurt_anim_playing: bool = false var hurt_anim_progress

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

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

这是上面的脚本:

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



实际上,我想设置动画位置,让动画在停止的地方继续,但是我得到了一个错误,问题是当前的动画位置只有一个getter,而不是setter:

要将动画设置为特定点,如果要处理从开始点到恢复点之间的所有内容,可以使用advance(浮点增量),如果只想跳到新位置,可以使用seek(浮点秒,bool update=false)。如果需要将update设置为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)

问题是当前位置只有一个getter,而不是setter:

要将动画设置为特定点,如果要处理从开始点到恢复点之间的所有内容,可以使用advance(浮点增量),如果只想跳到新位置,可以使用seek(浮点秒,bool update=false)。如果需要将update设置为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)

你得到了什么错误?
无效的设置索引“current\u animation\u position”(在基础上:“AnimationPlayer”)的值类型为“float”
如果它正在寻找一个float,你是否尝试过将你的hurt动画变量声明为
var hurt\u animation\u progress:float=0.0
?另外,如果这不起作用,我想知道你想做什么,另一种可能使事情变得更简单的方法是使用协同程序--yield()和resume()如果正在查找浮点值,则会出现什么错误?
无效的设置索引“current\u animation\u position”(基于“AnimationPlayer”)的值为“float”
,您是否尝试过将hurt动画变量声明为
var hurt\u anim\u progress:float=0.0
?此外,如果这不起作用,我认为对于您正在尝试的操作,另一种可能使事情更容易的方法是使用协程--yield()和resume()