微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动画只播放第一帧 Godot

如何解决动画只播放第一帧 Godot

我正在制作一个简单的平台游戏。我有 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

解决方法

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

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 语句,并且 run 和 walk 的每个滴答声都为真。例如,您按左键并移动。如果语句为真,则向左行走,动画设置为行走。然后 run if 语句也是 true 并且您的动画设置为运行。下一帧的步行动画 if 语句再次为真,这意味着它再次设置为步行动画。然后运行动画为真,它将动画设置为运行动画,但因为我们正在切换动画,它将帧设置为 0 并再次启动动画。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。