0 votes

First of all, i'd like to pologize for my bad english

Im doing the godot tutorial and i found a problem in the part where it teaches you how to flip your animation horizontally and vertically.

Whenever you try to make the player going both down and to some of the sides, the sprite flips vertically again, making it going down but pointing up at the same time.

Is there anyway i can fix that? Here is my code so far.

extends Area2D

# class member variables go here, for example:
# var a = 2
# var b = "textvar"

func _ready():
    screen_size = get_viewport_rect().size
    # Called when the node is added to the scene for the first time.
    # Initialization here
    pass

#func _process(delta):
#   # Called every frame. Delta is time since last frame.
#   # Update game logic here.
#   pass

export var speed = 400 #vel em pixel/segundo
var screen_size #tamanho da tela

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()
    position += velocity * delta
    position.x = clamp(position.x, 0, screen_size.x)
    position.y = clamp(position.y, 0, screen_size.y)

    if velocity.x != 0:
        $AnimatedSprite.animation = "right"
        $AnimatedSprite.flip_v = false
        #See the note below about boolean assignment
        $AnimatedSprite.flip_h = velocity.x < 0
    elif velocity.y != 0:
        $AnimatedSprite.animation = "up"
        $AnimatedSprite.flip_v = velocity.y > 0
in Engine by (12 points)
retagged by

1 Answer

+1 vote

It's because of this line I think :

$AnimatedSprite.flip_v = false

What happens is that you flip your sprite vertically when you are going down but if you press right at the same time you reset the vertical flip. One way to have your player facing up when you walk left and right only would be to check if your player has a velocity like this :

$AnimatedSprite.flip_v = false if velocity.y == 0 else $AnimatedSprite.flip_v
by (81 points)

thanks, you helped me a lot

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.