1

所以,我开始学习 Godot 游戏引擎,并选择了“你的第一个游戏”教程,(链接:http ://docs.godotengine.org/en/latest/getting_started/step_by_step/your_first_game.html )具体来说,“选择动画”部分。这是他们给出的代码:

if velocity.x != 0:
$AnimatedSprite.animation = "right"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = velocity.x < 0

elif velocity.y != 0:
    $AnimatedSprite.animation = "up"
    $AnimatedSprite.flip_v = velocity.y > 0

我得到的是,当按下“上”或“下”键时,精灵消失了,但一直移动到按下“左”或“右”时,它才重新出现。我还注意到它不会垂直翻转。

我修复了缺少精灵的问题,如下所示:

if velocity.x != 0:
    $AnimatedSprite.animation = "right"
    $AnimatedSprite.flip_v = false
    $AnimatedSprite.flip_h = velocity.x < 0 
elif velocity.x != 0:
    $AnimatedSprite.animation = "up"
    $AnimatedSprite.flip_v = velocity.y > 0

但是还有一个问题,它不会垂直改变。还有其他方法可以解决这个问题吗?

4

1 回答 1

0

In the Player script the velocity.y value is controlled with the ui_up and ui_down inputs as shown here:

func _process(delta):
    var velocity = Vector2() # The player's movement vector.
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    if velocity.length() > 0:
        velocity = velocity.normalized() * speed
        $AnimatedSprite.play()
    else:
        $AnimatedSprite.stop()

To have the behaviour you describe in your question either something is different with your implementation of this method or your velocity.y value is being set somewhere else.

于 2018-07-18T10:12:39.580 回答