Topic was automatically imported from the old Question2Answer platform.
Asked By
Timofey
I have a character (2d kinematic body) which consists of several sprites, collisions and animation nodes, and other scenes may be added to it in the future. I need to reflect all this on the axis of height and back, depending on where the character is moving.
func move(speed: float) -> void:
var velocity := Vector2.ZERO
if Input.is_action_pressed("p1Up"):
velocity.y = -1
if Input.is_action_pressed("p1Left"):
velocity.x = -1
if Input.is_action_pressed("p1Down"):
velocity.y = 1
if Input.is_action_pressed("p1Right"):
velocity.x = 1
move_and_slide(velocity * speed)
Potential solution 1
Find all of the Sprites and flip them:
# This will be true if the player's trying to move to the left.
# If the player's not trying to move to the left, then it will be
# false.
var flip_h = velocity.x < 0
for child in get_children():
if child is Sprite:
child.set_flip_h(flip_h)
Potential solution 2
This is probably the better way to do it. First, make sure that all of the Sprites that you want to flip are in a group named “to_flip”. Then:
# This will be true if the player's trying to move to the left. If
# the player's not trying to move to the left, then it will be
# false.
var flip_h = velocity.x < 0
get_tree().call_group("to_flip", "set_flip_h", flip_h)