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.