我正在写一部JavaScript的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“未定义。(世界资源)在org.mozilla.javascript.ScriptRuntime.constructError(Unknown来源)在org.mozilla.javascript.ScriptRuntime.constructError(Unknown源头)在org.mozilla.javascript.ScriptRuntime.notFoundError(Unknown源头)在org.mozilla.javascript.ScriptRuntime.name(Unknown源头)在org.mozilla.javascript.Interpreter.interpretLoop(Unknown源头)在script.Undo(世界Editor.js:1369)在script.procCmd(世界Editor.js:451)(在org.mozilla.javascript.Interpreter.interpret(Unknown来源)在org.mozilla.javascript.InterpretedFunction.call(Unknown来源)在org.mozilla.javascript.ContextFactory.doTopCall(Unknown来源)在org.mozilla.javascript.ScriptRuntime.doTopCall(Unknown源头)在org.mozilla.javascript.InterpretedFunction.call(Unknown源头)在net.zhuoweizhang.mcpelauncher.ScriptManager.callScriptMethod(ScriptManager.java:288) at net.zhuoweizhang.mcpelauncher.ScriptManager.chatCallback(ScriptManager.java:516)
我尝试过许多不同的方法,为undo
数组的每个元素分配数组,并将具有多个属性的对象分配给undo数组元素中包含的数组,但始终会出现此错误。
为什么会发生此错误,以及如何防止它?
发布于 2018-01-14 23:27:29
我猜你需要加上这一行:
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(undo[i][a].x, undo[i][a].y, undo[i][a].z),
metadata:Level.getData(undo[i][a].x, undo[i][a].y, undo[i][a].z)}); // <-- changed those 2 lines
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.");
}
}
发布于 2018-01-15 03:37:56
我太专注于这个对象以至于我忘了看那些函数.
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(undo[i][a].x, undo[i][a].y, undo[i][a].z),
metadata:Level.getData(undo[i][a].x, undo[i][a].y, undo[i][a].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.");
}
}
我需要在x
、y
和z
前面加上undo[i][a].
https://stackoverflow.com/questions/48252575
复制相似问题