我想完成This tutorial關於使用JavaScript和HTML5製作一個tic tac toe遊戲。未捕獲的參考錯誤:未定義繪製
我已多次跟蹤視頻中的每一步。雖然我的代碼似乎與視頻中的代碼相匹配,但我仍然遇到一個錯誤:Uncaught Reference Error:未定義繪製。錯誤發生在第12行。
我必須俯視一些東西。任何人都可以指出?
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe!</title>
<script type="text/javascript">
var c, canvas;
var turn = 1;
window.onload = function() {
canvas = document.getElementById("canvas");
c = canvas.getContext("2d");
draw();
}
var moves = [];
window.onclick = function(e) {
if(e.pageX < 500 && e.pageY < 500) {
var cX = Math.floor(e.pageX/(500/3));
var cY = Math.floor(e.pageY/(500/3));
var alreadyClicked = false;
for(i in moves) {
if(moves[i][0] == cX && moves[i][1] == cY) {
alreadyClicked = true;
}
}
if(alreadyClicked == false) {
moves[(moves.length)] = [cX, cY, turn];
turn = turn * -1;
draw();
}
}
var bg = new Image();
var x = new Image();
var o = new Image();
bg.src = "ttt_board.png";
x.src = "ttt_x.png";
o.src = "ttt_o.png";
function draw() {
c.clearRect(0, 0, 500, 500);
c.drawImage(bg, 0, 0);
for(i in moves) {
if(moves[i][2] == 1) {
c.drawImage(x, Math.floor(moves[i][0] * (500/3) + 10), Math.floor(moves[i][1](500/3) + 10))
} else {
c.drawImage(o, Math.floor(moves[i][0] * (500/3) + 10), Math.floor(moves[i][1] * (500/3) + 10));
}
}
}
};
</script>
</head>
<body>
<canvas id="canvas" width="500px" height="500px"></canvas>
</body>
</html>
我已經正確縮進了你的javascript。現在應該很明顯爲什麼函數沒有定義(不同的範圍)。除此之外:請確保所有的循環變量都是本地的(使用例如'var i;')! – ThiefMaster
正確嗎?甚至沒有關閉:o – Virus721