How to properly change the sprite depending on facing direction and other situations?

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

So I’m extremely new to programming, having just followed along a tutorial on how to make a simple platformer in Godot (and taking some basic python lessons beforehand). So decided to make my own with my own assets. Unfortunately the tutorial didn’t cover anything like animation or how to change the player sprite (except how to flip it).

So my problem is I found is the sprites only change when I move left or right. If I just jump, it’s stays in the animation for being on the ground. And if I move and jump, but stop moving before I touch the ground, the sprite stays the same for the falling sprite. That is until I move left or right, which goes back to the on ground sprites.

I added some stuff to try and fix it but I have no idea how to tell the game to check which sprite the player is currently showing. If I knew that I have a feeling I could get it to act the way I want. The stuff I added just… makes the player always facing left.

var ballRight = preload("res://Visuals/Ball Man Idle.png")
var ballLeft = preload("res://Visuals/Ball Man Idle Left.png")
var ballJumpRight = preload("res://Visuals/Ball Man Jump Up.png")
var ballJumpLeft = preload("res://Visuals/Ball Man Jump Up Left.png")
var ballFallRight = preload("res://Visuals/Ball Man Jump Down.png")
var ballFallLeft = preload("res://Visuals/Ball Man Jump Down Left.png")

func _physics_process(delta):
    	if Input.is_action_pressed("Right") and is_on_floor():
		 sprite.set_texture(ballRight)
	elif Input.is_action_pressed("Left") and is_on_floor():
		sprite.set_texture(ballLeft)
	elif Input.is_action_pressed("Right"):
		if vel.y < 0:
			sprite.set_texture(ballJumpRight)
		elif vel.y > 0:
			sprite.set_texture(ballFallRight)
	elif Input.is_action_pressed("Left"):
		if vel.y < 0:
			sprite.set_texture(ballJumpLeft)
		elif vel.y > 0:
			sprite.set_texture(ballFallLeft)
	elif sprite.set_texture(ballRight):
		if vel.y > 0:
			sprite.set_texture(ballFallRight)
		elif Input.is_action_just_pressed("Jump"):
			sprite.set_texture(ballJumpRight)
	elif sprite.set_texture(ballLeft):
		if vel.y > 0:
			sprite.set_texture(ballFallLeft)
		elif Input.is_action_just_pressed("Jump"):
			sprite.set_texture(ballJumpLeft)

A friend of mine that I didn’t even know coding figured it out for me.

All I needed to add was a variable that would change every time left or right is pressed. If right is pressed, the variable = 1, and of left is pressed, variable = 0. With that I could tell the game how to keep track of that and fixed everything to work as I wanted.

superBlast | 2020-07-05 16:05

1 Like