Flipping

When my character is walking left instead of right, how can I flip my sprite horizontally so it faces the way it is walking?
In this example, we will create a simple character that can walk around the screen. Since our image is drawn facing right, when they are walking left, we will flip the sprite horizontally.
To do this, we will create a new property called Facing and set it to 1 when the character is facing right,
and -1 when the character is facing left, and then we will make the ImageSprite respond to it accordingly.
Step 1: Create a Facing property
Create a new property called Facing and set it to 1 by default:
pub prop unit.Facing = 1
pub prop unit.IsWalking = false
To make this example more complete, we have also added a property called IsWalking which will be set to true when the character is walking,
enabling us to switch between a walking and idle animation.
Step 2: Update the Facing property
Every tick, we will check the joystick input and update the Facing property accordingly:
pub fn unit.Hero([owner]) {
// ...
// set the walking/facing state based on the joystick input
on BeforePhysics {
if Joystick == @(0, 0) {
IsWalking = false
} else {
IsWalking = true
Move(0.1 * Joystick)
if Joystick.X != 0 { // must be non-zero so we don't zero out the facing when moving vertically
Facing = Joystick.X.Sign
}
}
}
}
The Sign function returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
We use it to set the Facing property to 1 when the joystick is pushed right, and -1 when it is pushed left.
When the joystick is pushed up or down, we don't change the Facing property,
so the character continues to face the last direction it was moving in.
Even though we are using the Joystick input in this example,
the arrow keys will also work because they are automatically mapped to Joystick.
See Input Unification to learn more about this.
Step 3: React to the Facing property
The way to flip a sprite is to use the bodyScale parameter.
By default, bodyScale=@(1,1) which means the sprite is drawn normally.
Changing the X-component to -1, i.e. bodyScale=@(-1,1) will flip the sprite horizontally.
pub fn unit.Hero([owner]) {
use body=this, radius=1, shape=Rectangle(width=radius, height=2*radius)
Body(pos=@(0, 0))
// react to property changes using a `with` block
with IsWalking, Facing {
let bodyScale = @(Facing, 1) // flips the character horizontally when Facing is -1
ImageSprite(
image = IsWalking ? @character_maleAdventurer_walk*.png : @character_maleAdventurer_idle.png,
noRotation=true, // keeps our character upright
bodyScale=,
)
PolygonCollider(category=Category:Hero, bodyScale=)
}
}
Every time the Facing property changes,
we will need to update the bodyScale of the ImageSprite and PolygonCollider to match it.
To do this, we use a with block to automatically rerun the code
whenever the relevant properties change.
It's done! Now the character will flip horizontally when walking left or right.