What happened to flip_h?

I practice making RPG games from devworm’s channel, but I can’t run the code like he does. :face_holding_back_tears: :face_holding_back_tears:

This is all the code I used.
extends CharacterBody2D

const speed = 100
var current_dir = “none”

func _physics_process(delta):
player_movement(delta)

func player_movement(delta):

if Input.is_action_pressed("ui_right"):
	current_dir = "right"
	play_anim(1)
	velocity.x = speed
	velocity.y = 0
elif Input.is_action_pressed("ui_left"):
	current_dir = "left"
	play_anim(1)
	velocity.x = -speed
	velocity.y = 0
elif Input.is_action_pressed("ui_down"):
	current_dir = "down"
	play_anim(1)
	velocity.y = speed
	velocity.x = 0
elif Input.is_action_pressed("ui_up"):
	current_dir = "up"
	play_anim(1)
	velocity.y = -speed
	velocity.x = 0
else :
	play_anim(0)
	velocity.x = 0
	velocity.y = 0

move_and_slide()

func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D

if dir == "right":
	anim.flip_h = false
	if movement == 1:
		anim.play("side_walk")
	elif movement == 0:
		anim.play("side_idle")
if dir == "left":
	anim.flip_h = true
	if movement == 1:
		anim.play("side_walk")
	elif movement == 0:
		anim.play("side_idle")

if dir == "down":
	anim.flip_h = true
	if movement == 1:
		anim.play("front_walk")
	elif movement == 0:
		anim.play("front_idle")
if dir == "up":
	anim.flip_h = true
	if movement == 1:
		anim.play("back_walk")
	elif movement == 0:
		anim.play("back_idle")

สกรีนช็อต 2024-01-06 144414

Thank you for your help. :smiling_face_with_three_hearts:

What does your node tree look like for the player?

Is ‘anim’ a sprite or an animation player? I think you’re setting the animation player instead of the sprite. You should be flipping the sprite instead.

probably bad path or name… screenshot your scene tree where is your AnimatedSprite2D

or

@onready var anim = $AnimatedSprite2D

As others pointed out, if anim is null it means that there’s no node called “AnimatedSprite2D” that’s directly under the root node.

1 Like