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;
};
};
}
次の行に「UncaughtTypeError:object is not afunction」が表示されます。
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);
}
:のcoordinate
最初の行に同じ名前を付けることで、関数をシャドウイングしていますgetBaracade
。
var coordinate = getRandomBaracadeCoordinate();
getRandomBaracadeCoordinate()
返されるものはすべて関数ではないためnew coordinate
、エラーをスローします。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加