我收到了错误
未捕获的TypeError:无法设置未定义的属性"0"
出于某种原因,在这一行
world_map_array[i][z]="grass.gif|ongrass.gif|collision.gif|above.gif";
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
谢谢你的帮助
var x_world_map_tiles = 100;
var y_world_map_tiles = 100;
var world_map_array = new Array(x_world_map_tiles);
for (i=0; i<=2; i++)//create a two dimensional array so can access the map through x and y coords map_array[0][1] etc.
{
world_map_array[i]=new Array(y_world_map_tiles);
}
for (i=0; i<=x_world_map_tiles; i++)//just a test
{
for (z=0; z<=y_world_map_tiles; z++)//just a test
{
world_map_array[i][z]="grass.gif|ongrass.gif|collision.gif|above.gif";
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*hon 34
JavaScript中的数组有自己的怪癖,如果你来自其他语言,你可能不会期待.两个重要的用例是:
与其他语言不同,JavaScript不会为整个数组分配内存块.(它不知道你将在每个单元格中放置什么样的对象,因此它需要多少总内存.)相反,为你做的所有size参数Array()都设置了数组的length属性.
对于一般的2d阵列情况,我建议:
创建"顶部"数组,例如:
var i // the first-order index in a
, j // the second order index in a
, a = []
Run Code Online (Sandbox Code Playgroud)根据需要初始化数组元素.这称为延迟初始化,在这种情况下,它只涉及a[i]在我们尝试分配内容之前存在的测试a[i][j],例如:
if (!a[i]) a[i] = []
Run Code Online (Sandbox Code Playgroud)
在英语中,上述语句为:"如果第i个元素a是'falsy',则为第i个元素分配一个空数组."
最后,将实际值分配给multideminsional数组:
a[i][j] = 'whatever'
Run Code Online (Sandbox Code Playgroud)对于您的情况,您提前知道值,因此您可以提前初始化每个元素.(但是,如果你没有覆盖大多数元素,那么懒惰的实现可能会更好;见下文.)
var x, x_length = 100
, y, y_length = 100
, map = []
// Don't be lazy
for (x = 0; x < x_length; x++) {
map[x] = []
for (y = 0; y < y_length; y++) {
map[x][y] = 'grass.gif|ongrass.gif|collision.gif|above.gif'
}
}
Run Code Online (Sandbox Code Playgroud)
正如其他人所说的那样,一个包含100个元素的数组的索引编号从0到99,所以这里比较小的比较是最合适的.
作为参考,这是一个使用延迟初始化的实现.我已经使用了函数接口而不是直接访问数组; 它更长,更复杂,但也更完整.
我在这里使用的初始化模式称为 立即调用的函数表达式.如果您以前没有看过它,那么它是更有用的JavaScript模式之一,非常值得花一些时间来理解.
var map = (function (x_length, y_length, v_default, undefined) {
// Unless v_default is overwritten, use ...
v_default = v_default || 'grass.gif|ongrass.gif|collision.gif|above.gif'
// Private backing array; will contain only values for a[x][y]
// that were explicitly set.
var a = []
// Private helper function.
// - Returns `true` if `x` is between `0` and `x_length - 1`
// and `y` is between `0` and `y_length - 1`.
// - Returns `false` otherwise.
function valid (x, y) {
return (x >= 0
&& x < x_length
&& y >= 0
&& y < y_length)
}
// Private helper function.
// - Returns `true` if a[x][y] has been set().
// - Returns `false` otherwise.
function exists (x, y) {
return !!a[x] && !!a[x][y]
}
// Private getter
// - Returns the value of a[x][y] if it has been set().
// - Returns `undefined` if the point (x,y) is invalid.
// - Returns `v_default` otherwise.
function get (x, y) {
if (!valid(x, y)) return undefined
else if (exists(x, y)) return a[x][y]
else return v_default
}
// Private setter
// - Returns the value set on success.
// - Returns `undefined` on failure
function set (x, y, v) {
if (valid(x, y)) {
// We're being lazy
if (!a[x]) a[x] = []
a[x][y] = v
return a[x][y]
}
return undefined
}
// Return an interface function.
// - Pass the function three arguments, (x, y, v), to set a[x][y] = v
// - Pass the function two arguments, (x, y), to get a[x][y]
return function (x, y, v) {
if (arguments.length > 2) {
return set(x, y, v)
} else {
return get(x, y)
}
}
})(100, 100)
Run Code Online (Sandbox Code Playgroud)
当我在节点中运行上面的代码时,以下测试打印了合理的值:
// Invalid invocations
console.log('map() : %s', map())
console.log('map( 0) : %s', map(0))
console.log('map( -1, 0) : %s', map(-1,0))
console.log('map( 0, -1) : %s', map(0, -1))
console.log('map( -1, -1) : %s', map(-1, -1))
// Valid invocations
console.log('map( 0, 0) : %s', map(0, 0))
console.log('map( 99, 99) : %s', map(99, 99))
console.log('map( 1, 1) : %s', map(1,1))
console.log('map( 1, 1, "foo") : %s', map(1,1, 'foo'))
console.log('map( 1, 1) : %s', map(1,1))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68470 次 |
| 最近记录: |