【问题标题】:Animation plays only the first frame Godot动画只播放第一帧 Godot
【发布时间】:2021-05-25 07:40:41
【问题描述】:

我正在制作一个简单的平台游戏。我有 3 个玩家可以做的动作,跑、跳和走。步行和跳跃功能运行良好,但我的运行功能有问题,它似乎可以工作,但动画只播放第一帧,这不会发生在任何其他动画中。

extends KinematicBody2D

var speed : int = 200
var jumpforce: int = 500
var gravity: int = 1000

var vel: Vector2 = Vector2()

onready var sprite: Sprite = get_node("Sprite")
onready var anim = get_node("AnimationPlayer").get_animation("walk")
onready var anim2 = get_node("AnimationPlayer").get_animation("run")

func _physics_process(delta):
    vel.x = 0
    #movement
    if Input.is_action_pressed("move_left"):
        vel.x -= speed
        anim.set_loop(true)
        get_node("AnimationPlayer").play("walk")
        
    
    if Input.is_action_pressed("move_right"):
        vel.x += speed
        anim.set_loop(true)
        get_node("AnimationPlayer").play("walk")
        
        #return to idle
    if Input.is_action_just_released("move_right") or Input.is_action_just_released("move_left"):
        $AnimationPlayer.play("idle")
        #run
    if Input.is_action_pressed("move_left") and Input.is_action_pressed("shift"):
        vel.x -= speed
        anim2.set_loop(true)
        get_node("AnimationPlayer").play("run")
        
    if Input.is_action_pressed("move_right") and Input.is_action_pressed("shift"):
        vel.x += speed
        anim2.set_loop(true)
        get_node("AnimationPlayer").play("run")
    #physic and jump
    vel.y += gravity * delta
        
    if Input.is_action_pressed("jump") and is_on_floor():
        vel.y -= jumpforce
    
    vel = move_and_slide(vel, Vector2.UP)
    
    if vel.x < 0:
        sprite.flip_h = true
    elif vel.x > 0:
        sprite.flip_h = false

【问题讨论】:

    标签: godot gdscript


    【解决方案1】:

    嗨,伙计,我不确定您是否还需要帮助,但这是我的解决方案:

    extends KinematicBody2D
    
    var speed : int = 200
    var jumpforce: int = 500
    var gravity: int = 1000
    
    var vel: Vector2 = Vector2()
    
    onready var sprite: Sprite = get_node("Sprite")
    
    func _physics_process(delta):
        vel.x = 0
        #movement
            #run
        if Input.is_action_pressed("move_left") and Input.is_action_pressed("shift"):
            vel.x -= speed
            $AnimationPlayer.play("run")
        elif Input.is_action_pressed("move_right") and Input.is_action_pressed("shift"):
            vel.x += speed
            $AnimationPlayer.play("run")
        elif Input.is_action_pressed("move_left"):
            vel.x -= speed
            $AnimationPlayer.play("walk")
        elif Input.is_action_pressed("move_right"):
            vel.x += speed
            $AnimationPlayer.play("walk")
            #return to idle
        else:
            $AnimationPlayer.play("idle")
        
    
        #physic and jump
        vel.y += gravity * delta
            
        if Input.is_action_pressed("jump") and is_on_floor():
            vel.y -= jumpforce
        
        vel = move_and_slide(vel, Vector2.UP)
        
        if vel.x < 0:
            sprite.flip_h = true
        elif vel.x > 0:
            sprite.flip_h = false
    

    问题在于,您有一堆 if 语句,并且每次运行 if 语句都为 true 和 walk 。因此,例如,您按左键并换档。如果语句为真,则向左行走并且动画被设置为行走。然后运行 ​​if 语句也为真,并且您的动画被设置为运行。下一帧步行动画 if 语句再次为真,这意味着它再次设置为步行动画。然后运行动画为真,将动画设置为运行动画,但是因为我们正在切换动画,所以它将帧设置为 0 并再次启动动画。

    【讨论】:

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