1
votes

So, I started to learn the Godot Game Engine and picked the "Your First Game" tutorial, (link: http://docs.godotengine.org/en/latest/getting_started/step_by_step/your_first_game.html) specifically speaking, the "Choosing Animations" part. This is the code they give:

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

What I got is that when pressing the "up" or "down" key, the sprite disappeared but moved until pressing "left" or "right", that's when it reappeared. I also noticed it doesn't flip vertically.

I fixed the missing sprite problem, like this:

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

But there is one issue remaining, it doesn't change vertically. Is there another way to fix this?

1
Did you solve your issue in the meantime? Is it possible that you have to use velocity.y in your second if loop? Additionally, I would use a second if statement instead of an elif because this makes sure that the second statement gets executed as well if x and y doesn't equal 0.magenulcus

1 Answers

0
votes

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.