«Ожидается, что это будет использоваться методом класса» Я до сих пор не понимаю, - PullRequest
0 голосов
/ 04 февраля 2020

У меня ошибка от ESlint, но я не понимаю, почему. Я прочитал это:

и this:

Я до сих пор не понимаю, что я делаю не так.

Мой класс

/* eslint-disable no-plusplus */
/* eslint-disable no-undef */
class Player {
  constructor(imagePlayer, name, score, positionY, positionX) {
    this.imagePlayer = imagePlayer;
    this.name = name;
    this.score = score;
    this.x = positionX;
    this.y = positionY;
  }

  drawPlayer() {
    app.map.mapGame[this.y][this.x] = this.imagePlayer;
  }

  obstacle(y, x) {
    let colision = false;
    if (app.map.mapGame[y][x] === 1) {
      console.log("evaluación");
      colision = true;
    }
    return colision;
  }

  lastPosition(oldPosition, direction) {
    if (direction === left || direction === right) {
      app.map.mapGame[this.y][this.x - oldPosition] = 0;
    } else {
      app.map.mapGame[this.y - oldPosition][this.x] = 0;
    }
  }

  // movements players
  movement(direction) {
    switch (direction) {
      case up:
        if (this.y > 0) {
          if (this.obstacle(this.y - 1, this.x) === false) {
            this.y--;
            this.lastPosition(-1, direction);
          }
        }
        break;

      case down:
        if (this.y < 9) {
          if (this.obstacle(this.y + 1, this.x) === false) {
            this.y++;
            this.lastPosition(+1, direction);
          }
        }
        break;

      case left:
        if (this.x > 0) {
          this.x--;
          this.lastPosition(-1, direction);
        }
        break;

      case right:
        if (this.x < 14) {
          this.x++;
          this.lastPosition(+1, direction);
        }
        break;

      default:
        console.log("muro");
        break;
    }
  } // movement
}

Ошибка:
Ожидалось, что 'this' будет использоваться методом класса 'препятствие

Метод препятствий не является Рассмотрены полные дела только по двум из них.

1 Ответ

0 голосов
/ 05 февраля 2020

Линтер жалуется, что метод, который не использует экземпляр, для которого он был вызван (this), не должен быть методом экземпляра в первую очередь. Это плохая практика.

Вы либо

  • должны сделать его методом static, который будет называться Player.obstacle(x, y) (и, вероятно, будет переименован в checkGlobalMapForObstacle)
  • должен переместить метод в класс Map, к которому он принадлежит, так как он проверяет координаты относительно содержимого карты (this.mapGame[x][y]).
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...