Dodge The Creeps - sprites facing movement direction via different method

Godot Version

4.3

Question

Hello :slight_smile:
I’m trying to challenge myself by making the game “Dodge The Creeps” a different way than how they did in the tutorial.

Specifically I’m trying to make the player’s node animations play during movement, while also flipping the sprites to match the direction the player is facing.

My code:

extends Area2D

# Making Player Move 8-direction style with sprites facing direction of movement

var moveSpeed : int = 275
var moveDirection : Vector2

@onready var screenSize = get_viewport_rect().size #Gets the Vector2 of a Rect2 

@onready var animation = get_node("AnimatedSprite2D") # accessing node on _ready()

# ______________________________________________________________________________



func _physics_process(delta: float) -> void: # Use physics process for movement
	moveDirection.x = Input.get_axis("ui_left", "ui_right")
	moveDirection.y = Input.get_axis("ui_up", "ui_down")
	
	position += moveSpeed * moveDirection * delta # Movement equation
	position = position.clamp(Vector2.ZERO, screenSize) 
	
	# Clamp():
	# clamp() clamps the position of the current node to not exit the margin stated in it's args.
	# Takes min and max vector value. Good to bound player node to screen.
	
	# Problem code:
	if moveDirection != Vector2(0,0): # Vector2(0,0) means not moving in any dir
		animation.play() # player is moving, play

		if moveDirection == Vector2.LEFT: 
			animation.flip_h = true
			animation.play("Walk")
				
		elif moveDirection == Vector2.RIGHT: 
			animation.play("Walk")
	
		elif moveDirection == Vector2.UP: 
			animation.play("Up")
		
		elif moveDirection == Vector2.DOWN: 
			animation.flip_v = true
			animation.play("Up")

	else:
		animation.stop() # player isn't moving, stop


There aren’t any bugs, but the sprites only seem to flip the first time I press the appropriate buttons, when I know that _physics_process() is called every frame. So I’m definitely missing something, I just don’t know what.

Any help is much appreciated,
Thank you for reading

you have to use .flip_h = false if the direction is Right and .flip_v = false if the direction is down to flip the sprite back to its original orientation:

	# Problem code:
	if moveDirection != Vector2(0,0): # Vector2(0,0) means not moving in any dir
		animation.play() # player is moving, play

		if moveDirection == Vector2.LEFT: 
			animation.flip_h = true
			animation.play("Walk")
				
		elif moveDirection == Vector2.RIGHT: 
			animation.play("Walk")
			animation.flip_h = false
	
		elif moveDirection == Vector2.UP: 
			animation.play("Up")
			animation.flip_v = false

		elif moveDirection == Vector2.DOWN: 
			animation.flip_v = true
			animation.play("Up")

	else:
		animation.stop() # player isn't moving, stop
1 Like