【问题标题】:How do I skip to a position in the animation with an animationplayer in Godot?如何使用 Godot 中的动画播放器跳到动画中的某个位置?
【发布时间】:2019-06-02 14:37:50
【问题描述】:

我正在 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


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

【问题讨论】:

  • 你遇到了什么错误?
  • Invalid set index 'current_animation_position' (on base: 'AnimationPlayer') with value of type 'float'
  • 如果它正在寻找一个浮动,您是否尝试将您的伤害动画变量声明为var hurt_anim_progress: float = 0.0
  • 另外,如果这不起作用,我认为对于您正在尝试做的事情,另一种可能使事情变得更容易的方法是使用协程——yield() 和 resume()

标签: animation godot


【解决方案1】:

问题在于 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,您可以进行测试。文档说“如果更新为真,则动画也会更新,否则它会在处理时更新。”

您的代码将如下所示:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多