Создание снаряда за Javascript - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь воссоздать Space Invaders. Я застрял в создании снаряда с корабля игрока. Снаряд (желтый квадрат) движется без меня, нажимая клавишу «А».

Uncaught TypeError: Cannot set property 'directionY' of undefined
    at keyTyped (sketch.js:130)
    at Projectiles.shoot (sketch.js:137)
    at draw (sketch.js:32)
    at p5.redraw (p5.js:61731)
    at p5.<anonymous> (p5.js:55256)

Когда я нажимаю клавишу «А», появляется ошибка:

class Projectiles {
  constructor(color, positionX, positionY, sizeWidth = 50, sizeHeight = 25) {
    this.color = color;
    this.positionX = positionX;
    this.positionY = positionY;
    this.sizeWidth = sizeWidth;
    this.sizeHeight = sizeHeight;
    // default speed
    this.directionY = -5;

  }
  // create ship with default properties
  createShip() {
    fill(this.color);
    rect(this.positionX, this.positionY, this.sizeWidth, this.sizeHeight);
  }
  shoot() {
    this.positionY = this.positionY + this.directionY;
    // 
    function keyTyped() {
      if (key === 'a') {
        this.directionY = -5;
      } else if (key === 'b') {
        this.directionY = 5;
      }
      // uncomment to prevent any default behavior
      // return false;
    }
    keyTyped();

  }
}
...