Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/excaliburjs/sample-platformer. Sample Platformer built in Excalibur excaliburjs.com/sample-platformer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import * as ex from "excalibur";import { Resources, PlayerGraphics } from "./resources";import { collisionGroups, makeCharacterCollider } from "./physics";import { Config } from "./config";
export class Player extends ex.Actor { public onGround = true; public jumped = false; constructor(x: number, y: number) { super({ name: "Player", pos: new ex.Vector(x, y), z: 99, collisionGroup: collisionGroups.player, collisionType: ex.CollisionType.Active, collider: makeCharacterCollider(), }); }
onInitialize() { this.graphics.add("idle", PlayerGraphics.idle); this.graphics.add("left", PlayerGraphics.left); this.graphics.add("right", PlayerGraphics.right);
this.on("postcollision", (evt) => this.onPostCollision(evt)); }
onPostCollision(evt: ex.PostCollisionEvent) { if (evt.side === ex.Side.Bottom) { this.onGround = true; } }
onPreUpdate(engine: ex.Engine) { this._handleMovement(engine); this._updateAnimation(); }
private _handleMovement(engine: ex.Engine) { this.vel.x = 0;
if (engine.input.keyboard.isHeld(ex.Input.Keys.Left)) { this.vel.x = -Config.playerMoveSpeed; }
if (engine.input.keyboard.isHeld(ex.Input.Keys.Right)) { this.vel.x = Config.playerMoveSpeed; }
if (engine.input.keyboard.wasPressed(ex.Input.Keys.Up) && this.onGround) { this.vel.y = Config.playerJumpVelocity; this.onGround = false; Resources.jump.play(Config.fxVolume); } }
private _updateAnimation() { if (this.vel.x < 0) { this.graphics.use("left"); } if (this.vel.x > 0) { this.graphics.use("right"); } if (this.vel.x === 0) { this.graphics.use("idle"); } }}