help with godot tutorial

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NotAGoat

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

#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
	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
:bust_in_silhouette: Reply From: Thewolfs

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

thanks, you helped me a lot

NotAGoat | 2019-06-21 16:20