3

I have an error from ESlint, but I don´t understand why. I have read these:

and this:

I still don't understanding what am I doing wrong.

My class

/* 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
}

Error:
Expected 'this' to be used by class method 'obstacle

The obstacle method isn't settled on the full cases just on two of them.

boj
  • 10,935
  • 5
  • 38
  • 57
Marfaldo
  • 53
  • 1
  • 5
  • @Ambrown Expected 'this' to be used by class method 'obstacle – Marfaldo Feb 04 '20 at 21:21
  • I guess you could have obstacle() and use this.x and this.y instead of asking a new position!? – B. Go Feb 05 '20 at 00:18
  • @B.Go Thanks. I thought the same, but when I used this.x the player does not detect the obstacle. On the other hand, I need to know the new position to avoid the obstacles. – Marfaldo Feb 05 '20 at 08:59

1 Answers1

5

The linter is complaining that a method that doesn't use the instance it was called on (this) shouldn't be an instance method in the first place. It's a bad practice.

You either

  • should make it a static method, to be called as Player.obstacle(x, y) (and probably be renamed to checkGlobalMapForObstacle)
  • should move the method to the Map class where it belongs, as it is checking coordinates against the map contents (this.mapGame[x][y]).
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks. I was wondering if that way was the best, actually I just was doing some test and trying to find the best way to do it (best practice too). Maybe I had posted my solution to fast, but it was only to refer to the ESlint problem, not to point it as a good solution. – Marfaldo Feb 05 '20 at 20:02
  • I have a doubt should I creat a static method or a normal method. – Marfaldo Feb 05 '20 at 20:17
  • On `Map`, you would create a normal method, that would use `this.mapGame[x][y]` – Bergi Feb 05 '20 at 20:19