我收到来自 ESlint 的错误,但我不明白为什么。我读过这些:
\n\n\n\n和这个:
\n\n\n\n我仍然不明白我做错了什么。
\n\n我的课
\n\n/* eslint-disable no-plusplus */\r\n/* eslint-disable no-undef */\r\nclass Player {\r\n constructor(imagePlayer, name, score, positionY, positionX) {\r\n this.imagePlayer = imagePlayer;\r\n this.name = name;\r\n this.score = score;\r\n this.x = positionX;\r\n this.y = positionY;\r\n }\r\n\r\n drawPlayer() {\r\n app.map.mapGame[this.y][this.x] = this.imagePlayer;\r\n }\r\n\r\n obstacle(y, x) {\r\n let colision = false;\r\n if (app.map.mapGame[y][x] === 1) {\r\n console.log("evaluaci\xc3\xb3n");\r\n colision = true;\r\n }\r\n return colision;\r\n }\r\n\r\n lastPosition(oldPosition, direction) {\r\n if (direction === left || direction === right) {\r\n app.map.mapGame[this.y][this.x - oldPosition] = 0;\r\n } else {\r\n app.map.mapGame[this.y - oldPosition][this.x] = 0;\r\n }\r\n }\r\n\r\n // movements players\r\n movement(direction) {\r\n switch (direction) {\r\n case up:\r\n if (this.y > 0) {\r\n if (this.obstacle(this.y - 1, this.x) === false) {\r\n this.y--;\r\n this.lastPosition(-1, direction);\r\n }\r\n }\r\n break;\r\n\r\n case down:\r\n if (this.y < 9) {\r\n if (this.obstacle(this.y + 1, this.x) === false) {\r\n this.y++;\r\n this.lastPosition(+1, direction);\r\n }\r\n }\r\n break;\r\n\r\n case left:\r\n if (this.x > 0) {\r\n this.x--;\r\n this.lastPosition(-1, direction);\r\n }\r\n break;\r\n\r\n case right:\r\n if (this.x < 14) {\r\n this.x++;\r\n this.lastPosition(+1, direction);\r\n }\r\n break;\r\n\r\n default:\r\n console.log("muro");\r\n break;\r\n }\r\n } // movement\r\n}Run Code Online (Sandbox Code Playgroud)\r\n\n\n\n错误:
\n
\n 预期类方法\'obstacle 使用\'this\'
障碍法并没有解决全部情况,仅解决其中的两个问题。
\nlinter 抱怨说,不使用调用它的实例的方法 ( this) 首先就不应该是实例方法。这是一个不好的做法。
你要么
static方法,被称为Player.obstacle(x, y)(并且可能被重命名为checkGlobalMapForObstacle)Map它所属的类,因为它正在根据地图内容检查坐标 ( this.mapGame[x][y])。| 归档时间: |
|
| 查看次数: |
9987 次 |
| 最近记录: |