[SOLVED] How to make it so the code can tell if my sprite is facing left or right?

Hello I am making a 2d platformer similar to the likes of Mario as per our beginner requirements. However when my sprite is facing left or landing when facing left, it always jumps/lands facing right because I do not know how to make it so it knows when to play either left or right sprite animations.

Sorry for the inefficiency in the code, I would just like to make it work.

var Movement = pc.createScript('movement');
var duckspeed = 6.1;
var force = 5.3;
var onGround = true;

// initialize code called once per entity
Movement.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

// update code called every frame
Movement.prototype.update = function(dt) {

    if(this.app.keyboard.isPressed(pc.KEY_D)&& onGround === true) {
        this.entity.sprite.play("DuckRight");
        this.entity.rigidbody.applyForce(duckspeed,0,0);
    }
    
    if(this.app.keyboard.wasReleased(pc.KEY_D)&& onGround === true) {
        this.entity.sprite.play("DuckIdleRight");
    }
    
    if(this.app.keyboard.isPressed(pc.KEY_A)&& onGround === true) {
        this.entity.sprite.play("DuckLeft");
        this.entity.rigidbody.applyForce(-duckspeed,0,0);
    }
    
    if(this.app.keyboard.wasReleased(pc.KEY_A)&& onGround === true) {
        this.entity.sprite.play("DuckIdleLeft");
        
    }
    
    if(this.app.keyboard.isPressed(pc.KEY_SPACE)&& onGround === true)   {
        this.entity.rigidbody.applyImpulse(0,force, 0);
        this.entity.sprite.play("DuckJumpRight");
        onGround = false;
    }
    
    if(this.app.keyboard.isPressed(pc.KEY_D)&& onGround === false) {
        this.entity.rigidbody.applyForce(duckspeed,0, 0);
        this.entity.sprite.play("DuckJumpRight");
        onGround = false;
    }
    
    if(this.app.keyboard.isPressed(pc.KEY_A)&& onGround === false) {
        this.entity.rigidbody.applyForce(-duckspeed,0, 0);
        this.entity.sprite.play("DuckJumpLeft");
        onGround = false;
    }
    
};

Movement.prototype.onCollisionStart = function(result) {
    
    if (result.other.tags.has('ground')) {
        onGround = true;
        this.entity.sprite.play("DuckIdleRight");

    }
    
};

I know I’ve made it play Right sided animations but I don’t know how to make it go the other way around as I may be unfamiliar with the statements.

Hi @van999 and welcome! Interesting question. Since you do know the direction before the player jumps, I think it’s easiest to store that direction in a variable or bool.

// initialize
this.goingRight = true;
// update
if (this.app.keyboard.isPressed(pc.KEY_A) {
    this.goingRight = false;
}

if (this.app.keyboard.isPressed(pc.KEY_D) {
    this.goingRight = true;
}
// collision
Movement.prototype.onCollisionStart = function(result) {
    if (result.other.tags.has('ground')) {
        onGround = true;

        if (this.goingRight) {
            this.entity.sprite.play("DuckIdleRight");
        }
        else {
            this.entity.sprite.play("DuckIdleLeft");
        }
    }  
};
1 Like

This have helped with after jumping and falling down, it now faces the correct way. The only problem that I have is how do I use the goingRight variable to know if my sprite will jump left or right.

if(this.app.keyboard.isPressed(pc.KEY_SPACE)&& onGround === true)   {
        this.entity.rigidbody.applyImpulse(0,force, 0);
        this.entity.sprite.play("DuckJumpRight");
        onGround = false;

How would I go about sneaking in the goingRight?

The same way as in the collision function.

if (this.app.keyboard.isPressed(pc.KEY_SPACE) && onGround === true)   {
    this.entity.rigidbody.applyImpulse(0,force, 0);
    onGround = false;

    if (this.goingRight) {
       this.entity.sprite.play("DuckJumpRight");
    }
    else {
        this.entity.sprite.play("DuckJumpLeft");
    }
}
2 Likes

It doesn’t seem to work as intended. I’ve tweaked your statement.
Here is what fixed it:

 if(this.app.keyboard.isPressed(pc.KEY_SPACE) && onGround === true)   {
        this.entity.rigidbody.applyImpulse(0,force, 0);
        onGround = false;

        if(goingRight === true) {
            this.entity.sprite.play("DuckJumpRight");
    }
        else {
        this.entity.sprite.play("DuckJumpLeft");
    }

Thanks for the big help!

You mean this.goingRight I guess? Otherwise where do you set goingRight?