|
|
|
|
Reply From: |
jgodfrey |
The answer probably depends on how you game is structured, and how your scene load works. However, one simple solution might be to store the player’s facing direction in an Autoload (singleton) script. Then, when the scene changes, just retrieve the facing direction from the singleton and switch the character direction as necessary when the new scene loads…
It worked! Thank you!!
I made a global variable called GlobalDir and gave it a variable called direction, set that equal to 1.
Then, in the Player script, I added an If statement that checks if x_input is < 0. If it is, the GlobalDir’s direction variable is updated to -1. Then the same for x_input > 0 and 1.
Next I created a function called SpawnCheck( ). It uses an If statement to see if GlobalDir’s direction is less than zero. If it is, then it sets the boolean flip_h from the sprite node to true.
Last, I added added the SpawnCheck( ) function to the physic process, so that it checks for it every frame.
Here is the code:
GlobalDir:
extends Node
var direction = 1
Player’s Physic Process:
func _physics_process(delta):
Spawn_Check()
var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
if x_input != 0:
animationPlayer.play("run")
motion.x += x_input * acceleration * delta
motion.x = clamp(motion.x, -max_speed, max_speed)
sprite.flip_h = x_input < 0
if x_input < 0:
GlobalDir.direction = -1
print(GlobalDir.direction)
if x_input > 0:
GlobalDir.direction = 1
print(GlobalDir.direction)
else:
animationPlayer.play("stand")
I did have another question if you are game for it. Do you know why the
“sprite.flip_h = x_input < 0”
line works (under If x_input != 0)?
Does the negative x_input value equate to a “false” in boolean terms?
theshirecat | 2020-11-22 17:02
Glad you have it working. Regarding your sprite.flip_h
question…,
To go through it a step at a time…
sprite.flip_h
requires a boolean value. That could be either a literal true
or false
or the result of an expression that resolves to a boolean value.
In the case of the mentioned code, your flip_h
value is being set as the result of an expression that resolves to a boolean. In that case, the expression is x_input < 0
.
You can kind of read that as a question. Is the current x input value < 0?. If it is, that expression resolves to true
. If is is not, that expression resolves to false
.
So, the expression is evaluated and resolved to either true
or false
. Once that’s done, the value is assigned back to your sprite.flip_h
property.
Does that help?
jgodfrey | 2020-11-22 17:15
Yes it does - thank you!
theshirecat | 2020-11-22 20:12