AnimatedSpriteFrame 的多个 CollisionShape2D

Several CollisionShape2D for a AnimatedSpriteFrame

我想为 AnimatedSprite 使用 CollisionPolygon2D 以完美地附加到每一帧,但我做不到。 有没有办法为每一帧定义一个唯一的 CollisionPolygon2D? 谢谢。

使用 AnimationPlayer 节点。然后您可以设置 CollisionShape.

的任何参数

为此:

  • Select AnimationPlayer 节点。
  • Select CollisionShape 节点。
  • 在检查器选项卡中,您现在可以在属性旁边看到一个小键。
  • Select形状看碰撞形状
  • 现在单击形状和 属性 名称旁边的按键。
  • AnimationPlayer 节点的动画中,时间轴周围有小菱形。
  • 在动画周围移动并将属性更改为所需值。

AnimatedSprite节点只关心图形。并且您需要 CollisionPolygon2D 作为物理对象的子对象(例如 KinematicBody2D)。因此,您需要其他东西来启动它们。

例如,您可以有多个 CollisionPolygon2D,每个 AnimatedSprite 帧一个。将它们全部禁用,并在物理对象的 _process 中,您只能为当前帧启用一个:

var prior_frame:int = 0

func _process(_delta:float) -> void:
    var sprite:AnimatedSprite = $AnimatedSprite

    # make sure the collision for the current frame is not disabled
    var curr_collision := get_node(str("Coll", sprite.frame)) as CollisionPolygon2D
    curr_collision.disabled = false

    # if the frame is the same we last seen, we are done
    if prior_frame == sprite.frame:
        return
    
    # the frame is different, thus disable the collision for the prior frame
    var old_collision := get_node(str("Coll", prior_frame)) as CollisionPolygon2D
    old_collision.disabled = true

    # keep track of the frame we have seen
    prior_frame = sprite.frame

以上代码假定您有名为 Coll0Coll1Coll2 等的节点。 AnimatedSprite.

每帧一个