我之前发布了一个类似的问题,但它被删除了,因为我觉得我在描述我的问题是什么方面做得很差。
请在此处查看我当前的计划:
我的 fiddle : http://jsfiddle.net/rich2233/JGvAD/
我正在尝试模拟这个游戏:http://javascript.internet.com/games/concentration.html
我的版本不会使用图像,而是使用简单的数字。游戏结束后,它会提醒用户需要尝试多少次才能匹配所有内容。它也不会记录时间。
我认为我的代码在逻辑上一切正常,但它无法运行。我是 JS 新手,我的调试技巧有限,但我似乎找不到任何语法错误。有人可以帮助让这个程序运行吗?我们将不胜感激。
var attempts = 0;
var firstClick;
var secondClick;
var numFoundPairs = 0;
var click = true;
function display(id)
{
attempts++;
if (click == false)
return;
var num = parseInt(id);
if (firstClick == null)
{
firstClick = num;
document.getElementById(id).value = a[num];
return;
}
if (secondClick == null)
secondClick = num;
if (firstClick == secondClick)
{
secondClick = null;
return;
}
document.getElementById(boxNum).value = a[num];
if (document.getElementById(firstClick).value !=
document.getElementById(secondClick).value )
{
click = false;
setTimeout ('hide(' + secondClick + ')', 3000);
setTimeout ('hide(' + firstClick + ')', 3000);
}
else
{
/* Show the identical blocks */
firstClick = null;
secondClick = null;
}
/* If the player found the eight pairs (since 16/2 = 8) */
if (numFoundPairs == 8)
{
/* Let them know how many attempts it took them */
alert ("You took " + Math.floor(String(attempts)/2) + " attempts.");
}
}
/* Function to hide the particular id */
function hide (id)
{
document.getElementById(id).value = " ";
click = true;
}
/* Create the array of number and distribute them randomly on the grid */
function create()
{
a = new Array (1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
6, 6, 7, 7, 8, 8);
for (var i = 0; i < a.length; i++)
{
var idx = Math.floor (Math.random() * a.length);
var tmp = a[i];
a[i] = a[idx];
a[idx] = tmp;
}
}
最佳答案
http://rich2233.comoj.com/处的错误发生在 memory.js 的第 28 行。
Uncaught ReferenceError :boxNum 未定义
您应该考虑使用 JavaScript 控制台,例如 Firebug (适用于 Firefox)或 Chrome 开发者工具(自带,按 CTRL+SHIFT+J 打开它们)。 Firefox 也有一个内置的 JavaScript 错误窗口。
它们会帮助您立即发现此类错误。
关于javascript - 为什么我的 JS 程序无法运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870677/