How to scale a Sprite through code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zambie135

I’m wanting to make a Fighter game in the Godot engine. The players move left and right.
I want to change the scale of the Sprite, whenever It walks to the left.
The reason I’m using scale and not"flip_h" is because the Sprite node is a parent node for an Area2D and CollisionShape2D node.

Is there a way to change the Sprite’s scale through code?( GDscript preferably)

:bust_in_silhouette: Reply From: kidscancode

You can change the scale property:

Examples:

node.scale = Vector2(1.5, 1.5)
node.scale.x = -1
node.scale.x *= -1

and so on.

thank you very much! This is working

Zambie135 | 2020-07-02 16:22

Note that you should be careful if you’re setting scale.x = -1 or 1 repeatedly in code.

See my answer on Why my character scale keep changing? - Archive - Godot Forum

To sum up, scale.x = -1 is converted into scale.y = -1 and rotation_degrees = 180, but scale.x remains 1 internally, so next time you set scale.x = -1, it will try to flip again.

Setting scale.x *= -1 whenever there is a true direction change will work though, as it will in practice just try to set scale.x = -1, and instead apply scale.y *= -1 and rotation_degrees = 180.

Hyper Sonic | 2023-02-08 18:49