【发布时间】:2015-02-06 20:06:23
【问题描述】:
出于学习目的,我正在尝试将我用 Java 制作的基本游戏转换为 HTML5;但是我遇到了一些麻烦,我会提供一个可运行的示例,但是我使用的是 Phaser 引擎,并且我的代码非常分散。我正在尝试在 Javascript 中遵循 OO 方法,无论哪种方式都是我的代码。
创建数组:
function newArray2D(rows, columns) {
var x = new Array(rows);
for(var i = 0; i < rows; i++) {
x[i] = new Array(columns);
}
return x;
}
-
tiles = newArray2D(Math.ceil(game.width / 64), Math.ceil(game.height / 64));
填充数组:
for(var x = 0; x < tiles.length; x++) {
for(var y = 0; y < tiles[0].length; y++) {
tiles[x][y] = new Tile(x * 64, y * 64);
}
}
Tile“类”:
var x, y, width, height;
function Tile(x, y) {
this.x = x;
this.y = y;
this.width = 64;
this.height = 64;
}
function hovering(mouseX, mouseY) {
if((mouseX > x && mouseX < (x + width)
&& mouseY > y && mouseY < (y + height))) {
return true;
}
return false;
}
错误:
Uncaught TypeError: undefined is not a function
导致错误的代码:
for(var x = 0; x < tiles.length; x++) {
for(var y = 0; y < tiles[0].length; y++) {
if(tiles[x][y].hovering(game.input.mousePointer.x, game.input.mousePointer.y)) {
tileDebug.text = 'Tile('+x+','+y+')';
}
}
}
触发错误的代码行:
if(tiles[x][y].hovering(game.input.mousePointer.x, game.input.mousePointer.y)) {
我不确定问题是什么,因为调试控制台并没有提供比“未定义不是函数”更多的信息......但我调用的函数是“悬停”,不是是吗?也许我创建数组错误?
【问题讨论】:
标签: javascript jquery arrays html phaser-framework