I want to provide an answer for Godot 3 users who are stumbling upon this page in 2018, like I did:
You can accomplish flipping an entire Node2d and all its children through use of apply_scale
.
Here is how I have horizontal flipping set up for my platform game:
const DIRECTION_RIGHT = 1
const DIRECTION_LEFT = -1
var direction = Vector2(DIRECTION_RIGHT, 1)
func set_direction(hor_direction):
if hor_direction == 0:
hor_direction = DIRECTION_RIGHT # default to right if param is 0
var hor_dir_mod = hor_direction / abs(hor_direction) # get unit direction
apply_scale(Vector2(hor_dir_mod * direction.x, 1)) # flip
direction = Vector2(hor_dir_mod, direction.y) # update direction
Then, when needed, just call:
set_direction(DIRECTION_LEFT)
or:
set_direction(DIRECTION_RIGHT)
The Node2d will then flip if it is facing the opposite direction of the parameter.
The nice thing about this implementation is that the var direction
can be applied to dependent scenes, such as when determining the direction of a projectile:
func create_projectile():
var projectile = PROJECTILE_SCENE.instance()
projectile.direction = direction
get_parent().add_child(projectile)
Note the above function does not define PROJECTILE_SCENE
or cover translating it to the correct coordinates - it is only to show the use of direction
.