我正在写一个游戏,到目前为止,我所拥有的代表游戏棋盘的对象是
// define game object
function Game ( board, numBlocks ) {
// board: Raphael object that the snake will live on
// numBlocks: Number of blocks both horizontally AND vertically -- the grid structure should be squares
this.board = board;
this.numBlocks = numBlocks;
this.snake; // Snake object on the board
this.openCoords = []; // coordinates on which the snake is not living at the moment
this.food = null; // food element on board
this.getAllCoords = function ( )
{
// returns list of all grid coordinates on the canvas,
// e.g. [{x:0,y:0}, {x:0,y:1}, ..., {x:15, y:15}] on a 16x16 board
var retList = [];
for (var i = 0; i < this.numBlocks; ++i)
for (var j = 0; j < this.numBlocks; ++j)
retList.push({ x : i, y : j });
return retList;
}
this.Snake = function ( )
{
// start with 3 blocks in the center-ish
var blockWidth = this.board.getSize().width / this.numBlocks;
var centerCoord = this.openCoords[this.openCoords.length / 2];
var headElement = new elementOnGrid(
board.rect(centerCoord.x * blockWidth, centerCoord.y * blockWidth, blockWidth, blockWidth, 5).attr('fill', '#19FF19'),
centerCoord.x,
centerCoord.y
);
}
this.elementOnGrid = function ( elem, xpos, ypos )
{
// elem: Rapael element (see: http://raphaeljs.com/reference.html#Element)
// xpos, ypos: x and y grid coordinates of the current position of the element
return { elem: elem, pos : [xpos, ypos] };
}
this.placeFood = function ( )
{
var randIndex = randInt(0, this.openCoords.length);
var randCoord = this.openCoords[randIndex]; // get random x-y grid coordinate from list of open coordinates
var blockWidth = this.board.getSize().width / this.numBlocks; // width in pixels of a block on the board
if (this.food == null) // if food element hasn't been initialized
{
// initialize the food element
this.food = new this.elementOnGrid(
board.circle(randCoord.x * blockWidth + blockWidth / 2, randCoord.y * blockWidth + blockWidth / 2, blockWidth / 2).attr('fill', '#cf6a4c'), // place circle in random location on the board (see http://raphaeljs.com/reference.html#Paper.circle)
randCoord.x,
randCoord.y
); // set food to be new element of type elementOnGrid
}
else // food element has been initialized (game is in play)
{
// move the food element
// ...
}
this.openCoords.splice(1, randIndex); // remove from openCoords the element that
}
this.startNew = function ( ) {
this.openCoords = this.getAllCoords();
this.snake = new this.Snake();
this.placeFood();
}
}
我的问题是我得到了一个
Uncaught TypeError: Cannot read property 'getSize' of undefined
在线
var blockWidth = this.board.getSize().width / this.numBlocks;
当我运行时
SG = new Game(snakeBoard, 16);
SG.startNew();
开始游戏。
我知道这是某种类型的作用域/继承/提升/等等。问题,但我无法准确指出问题所在。奇怪的是,我刚刚创建了 this.Snake ( ) { ... }
函数/对象,并且在此之前是行 var blockWidth = this.board.getSize().width/this.numBlocks;
在 this.placeFood ( ) { ... }
函数中没有引起问题,尽管我看不出它有什么根本的不同..
最佳答案
关于范围问题,您是正确的。你之前用 placeFood()
做的事情之间的区别以及你现在用 Snake()
做什么是 new
关键词。
new
关键字告诉函数管理它自己的 this
所以你的Game
上下文不再适用于 this
在 Snake
内功能。
一个解决方案是将上下文传递给 Snake
像这样的功能
this.snake = new this.Snake(this);
然后在 Snake
中管理上下文这样的构造函数
this.Snake = function(game) {
var blockWidth = game.board.getSize().width / game.numBlocks;
...
// replacing `this` with `game` for all relevant properties
参见 here有关 new
的更多信息关键字。
关于javascript - 需要帮助弄清楚为什么这个对象是未定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602659/