From 883eee1a815dd7f967922096f042f91b8d45d44f Mon Sep 17 00:00:00 2001 From: John Edvard Reiten Date: Wed, 17 Aug 2022 23:30:59 +0900 Subject: [PATCH] feat: Change player direction --- src/Player.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Player.js b/src/Player.js index fa4cdec..15d54d0 100644 --- a/src/Player.js +++ b/src/Player.js @@ -11,7 +11,7 @@ export class Player { rope = []; // list of pointmasses pointMass; playerControls; - sprite; + sprite = { render: () => {} }; constructor(x, y, game) { this.x = x; @@ -87,13 +87,20 @@ export class Player { } renderPlayer(_ctx) { - if (this.sprite) { - this.sprite.render(); - } + this.sprite.render(); } applyForce(fX, fY) { this.pointMass.applyForce(fX, fY); + this.changePlayerDirection(fX < 0); + } + + changePlayerDirection(isLeft) { + if (isLeft) { + this.sprite.scaleX = -2; + } else { + this.sprite.scaleX = 2; + } } render(ctx) { @@ -104,10 +111,9 @@ export class Player { update() { this.x = this.pointMass.x; this.y = this.pointMass.y; - if (this.sprite) { - this.sprite.x = this.pointMass.x; - this.sprite.y = this.pointMass.y; - } + + this.sprite.x = this.pointMass.x; + this.sprite.y = this.pointMass.y; this.updateRope(); this.dragRope();