【发布时间】:2015-06-08 07:02:00
【问题描述】:
function coordinate(x, y) {
this.x = x,
this.y = y
}
function generateBaracade(numBlocks) {
var coordinate = getRandomBaracadeCoordinate();
var xStyle = expandDirection.down;
var yStyle = expandDirection.right;
var xNumBlocks = findRandomFactor(numBlocks);
var yNumBlocks = numBlocks / xNumBlocks;
//figure out which way to expand the blocks
if (coordinate.x + xNumBlocks > xBlocks) {
xStyle = expandDirection.left;
} else {
xStyle = expandDirection.right;
}
if (coordinate.y + yNumBlocks > yBlocks) {
yStyle = expandDirection.down;
} else {
yStyle = expandDirection.up;
}
for (var i = 0; i <= xNumBlocks - 1; i++) {
for (var j = 0; j <= yNumBlocks - 1; j++) {
var tempBlock = Object.create(block);
tempBlock.type = "obstruction";
tempBlock.color = "grey";
tempBlock.illegalTerrain = true;
tempBlock.coordinate = new coordinate(coordinate.x + (i * xStyle), coordinate.y + (j * yStyle));
blockContainer[coordinate.x + (i * xStyle)][coordinate.y + (j * yStyle)] = tempBlock;
};
};
}
我得到'Uncaught TypeError: object is not a function'就行了:
tempBlock.coordinate = new coordinate(coordinate.x + (i * xStyle), coordinate.y + (j * yStyle));
这很奇怪,因为我正在按照 Mozilla 指南执行此操作:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
编辑:getRandomBaracadeCoordinate 的来源。 return 语句正是我想要做的,并且执行没有错误。
function getRandomBaracadeCoordinate() {
var x = Math.floor((Math.random() * (xBlocks)));
var y = Math.floor((Math.random() * (yBlocks)));
return new coordinate(x, y);
}
【问题讨论】:
标签: javascript