Godot - 角色动画重复循环(不断重新运行),绕过任何等待其完成的尝试

问题描述 投票:0回答:1

我的角色的死亡动画被重复激活,永远不允许动画完成,从而导致无休止的循环。我发现这是因为我正在检查的物理函数每秒被检查数百次,永远不允许动画在再次播放之前完成。

我尝试创建一个在特定条件下调用的单独函数,并尝试多次更改条件以查看是否可以避免循环。下面是我的两个主要函数,其中大多数动作发生的物理函数和死亡函数(正如你可以想象的那样)实现了与死亡相关的功能。我可以将代码嵌套在物理函数内的死亡函数中,但我认为将它们分开可能会解决问题。它没。我尝试检查死亡动画是否已经在播放,但这似乎没有影响:

if get_node("AnimatedSprite2D").animation != "Hurt":
我的下一个想法是“AnimatedSprite2D”和“AnimationPlayer”之间的差异导致了一些问题,导致它们要么改变动画,要么改变其他东西。我不确定如何继续,任何帮助/解释都将有助于帮助我更多地理解这个问题。如果您还有任何涉及其余代码的初学者友好建议,我会接受任何建设性的批评。谢谢!

func _physics_process(delta):
    if alive == true:
        if not is_on_floor():
            velocity.y += gravity * delta
        if Input.is_action_just_pressed("ui_accept") and is_on_floor():
            velocity.y = JUMP_VELOCITY
            if get_node("AnimatedSprite2D").animation != "Hurt":
                get_node("AnimationPlayer").play("Jump")
        var direction = Input.get_axis("ui_left", "ui_right")
        if direction == -1:
            get_node("AnimatedSprite2D").flip_h = true
        elif direction == 1:
            get_node("AnimatedSprite2D").flip_h = false
        if direction:
            velocity.x = direction * SPEED
            if velocity.y == 0:
                if get_node("AnimatedSprite2D").animation != "Hurt":
                    get_node("AnimationPlayer").play("Run")
        else:
            velocity.x = move_toward(velocity.x, 0, SPEED)
            if velocity.y == 0:
                if get_node("AnimatedSprite2D").animation != "Hurt":
                    get_node("AnimationPlayer").play("Idle")
        if velocity.y > 0:
            if get_node("AnimatedSprite2D").animation != "Hurt":
                get_node("AnimationPlayer").play("Fall")
    move_and_slide()
    if health <= 0:
        death()

func death():
    alive = false
    if get_node("AnimatedSprite2D").animation != "Hurt":
        get_node("AnimatedSprite2D").play("Hurt")
        await get_node("AnimatedSprite2D").animation_finished
        self.queue_free()
        get_tree().change_scene_to_file("res://main.tscn")
loops animation character godot
1个回答
0
投票

- 简单的解决方案

我能想到的更简单的解决方案就是颠倒死亡函数中检查的顺序。这样,如果角色还活着,代码仅运行一次(并且其他函数不会更改任何其他内容)。

func death():
    # Check your condition and if the character is alive
    if alive && condition:
        # Update the state of the character
        alive = false

        # The rest of your code goes here ...

- 真正的解决方案

现在谈论房间里的大象:为什么你的角色的检查会在物理代码中死亡?

你的函数不应该同时处理多个事情,从长远来看,这对于代码调试来说是一个问题,并且阅读起来很痛苦。应该有另一个函数来改变角色的生命,这应该是添加死亡函数调用的地方。

类似这样的:

func update_health(value: int):
    # Update the character's health
    health += value

    # Anything else you want to do...

    # Check if the character should die
    if health <= 0:
        death()

如果你知道自己在做什么,那么一个函数做“不止一件事”并没有什么问题,但它至少应该有一个明确且明确的目标。例如,更新角色动画、处理损坏或调用应每帧运行的其他函数。

© www.soinside.com 2019 - 2024. All rights reserved.