【发布时间】:2018-01-14 18:06:20
【问题描述】:
我正在编写一个 JavaScript Minecraft Blocklauncher 模组。
var undo = new Array(0);
var redo = new Array(0);
function Checkpoint(FromUI) {
if (undo === undefined) {
undo = new Array(0);
}
if (undo.length == 0) {
undo.push(new Array(0));
}
else if (undo[undo.length - 1].length > 0) {
undo.push(new Array(0));
if (undo.length > size) {
undo.shift();
}
}
redo = new Array(0);
if (FromUI !== undefined) {
Msg("Undo: " + undo.length + " level(s) available. Redo stack has been cleared.");
}
}
function SetBlock(x, y, z, id, metadata) {
if (!Array.isArray(undo[undo.length - 1])) {
undo[undo.length - 1] = new Array(0);
}
undo[undo.length - 1].push({'x': x, 'y': y, 'z': z, 'block': id, 'metadata': Level.getData(x, y, z)});
setTile(x, y, z, id, metadata);
}
function Undo() {
if (undo.length > 0) {
var i = undo.length - 1;
redo.push(new Array(0));
for (var a = undo[i].length - 1; a >= 0; a--) {
redo[redo.length - 1].push({x:undo[i][a].x,
y:undo[i][a].y,
z:undo[i][a].z,
block:getTile(x, y, z),
metadata:Level.getData(x, y, z)});
setTile(undo[i][a].x, undo[i][a].y, undo[i][a].z, undo[i][a].block, undo[i][a].metadata);
}
undo.pop();
Msg("Undo successful. " + undo.length + " undo level(s) remaining. " + redo.length + " redo level(s) available.");
} else {
Msg("Nothing to undo. " + redo.length + " redo level(s) available.");
}
}
Checkpoint() 被调用一次,然后SetBlock() 被多次调用。调用undo() 时,会发生错误。
错误行是:
redo[redo.length - 1].push({x:undo[i][a].x,
y:undo[i][a].y,
z:undo[i][a].z,
block:getTile(x, y, z),
metadata:Level.getData(x, y, z)});
与:
脚本中发生错误:World Editor.js org.mozilla.javascript.EcmaError: ReferenceError: "x" 未定义。 (World Editor.js#1369) 在 org.mozilla.javascript.ScriptRuntime.constructError(未知来源) 在 org.mozilla.javascript.ScriptRuntime.constructError(未知 来源)在 org.mozilla.javascript.ScriptRuntime.notFoundError(Unknown 来源)在 org.mozilla.javascript.ScriptRuntime.name(未知来源) 在 org.mozilla.javascript.Interpreter.interpretLoop(未知来源) 在 script.Undo(World Editor.js:1369) 在 script.procCmd(World Editor.js:451) 在 org.mozilla.javascript.Interpreter.interpret(未知来源)在 org.mozilla.javascript.InterpretedFunction.call(Unknown Source) at org.mozilla.javascript.ContextFactory.doTopCall(未知来源)在 org.mozilla.javascript.ScriptRuntime.doTopCall(未知来源)在 org.mozilla.javascript.InterpretedFunction.call(Unknown Source) at net.zhuoweizhang.mcpelauncher.ScriptManager.callScriptMethod(ScriptManager.java:288) 在 net.zhuoweizhang.mcpelauncher.ScriptManager.chatCallback(ScriptManager.java:516)
我尝试了许多不同的方法来将数组分配给undo 数组的每个元素,并将具有多个属性的对象分配给包含在撤消数组元素中的数组,但总是出现此错误。
为什么会出现这个错误,我该如何预防?
【问题讨论】:
-
发生在哪一行错误?
-
如前所述:
redo[redo.length - 1].push({x:undo[i][a].x, y:undo[i][a].y, z:undo[i][a].z, block:getTile(x, y, z), metadata:Level.getData(x, y, z)}); -
"x" is not defined.对我来说似乎很清楚。这个x:undo[i][a].x没有声明变量x,而是一个属性。 -
@Slai,x 不是在
SetBlock()中定义在undo[undo.length - 1].push({'x': x, 'y': y, 'z': z, 'block': id, 'metadata': Level.getData(x, y, z)}); setTile(x, y, z, id, metadata);中吗? -
no .. 变量声明将具有等号,如
x = ...
标签: javascript arrays object multidimensional-array