This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello!

I'm working on a 2d platformer and having trouble with sprite animations.

When I run to the right, I transition to the next room and I am facing the right when the scene loads.

When I run back from where I came to the left, I am facing the right upon the scene loading, even though I was just running to the left.

Is there a way to make the sprite direction update with the scene change?

I've been pulling my hair out trying to get this to work.

in Engine by (17 points)

1 Answer

0 votes
Best answer

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...

by (22,704 points)
selected by

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 xinput is < 0. If it is, the GlobalDir's direction variable is updated to -1. Then the same for xinput > 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.fliph = xinput < 0"

line works (under If xinput != 0)?
Does the negative x
input value equate to a "false" in boolean terms?

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?

Yes it does - thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.