我無法解釋爲什麼下面的「buttonHandler」函數中的第一行不起作用,畫布在此語句後不顯示。然而,「startPlay」函數的第一行是相同的,並顯示畫布!如果有人能看到爲什麼從這段代碼請讓我知道..canvas.style.display =「block」不起作用
謝謝。
我已經在Chrome和Firefox中試過這個,結果相同。
function buttonHandler(){
canvas.style.display = "block";
var menuDisplay = document.getElementById("menuDisplay");
menuDisplay.style.display = "none";
drawingSurface.font = readyDisplay.font;
drawingSurface.fillStyle = readyDisplay.fillStyle;
drawingSurface.textBaseline = readyDisplay.textBaseline;
drawingSurface.fillText(readyDisplay.text, readyDisplay.x, readyDisplay.y);
window.setTimeout("startPlay()", 3000);
}
function startPlay(){
canvas.style.display = "block";
gameOver.style.display = "none";
balls=[];
for(var i=0;i < sprites.length; i++){
var thisSprite = sprites[i];
if(thisSprite !== cup){
removeObject(thisSprite, sprites);
}
}
score = 0;
totalSeconds = 5;
gameState = PLAYING;
drawingSurface.clearRect(0, 0, canvas.width, canvas.height);
window.setTimeout("tick()", 1000); // Start the countdown timer
timeToBall = Math.floor(Math.random() * 250) + 50;
update();
}
需要完整的代碼才能看到。 Jsfiddle適合我們。 – Papa
絕對不要在字符串中使用'setTimeout' /'setInterval',因爲那時它們會使用邪惡的'eval'!改用一個函數的引用('setTimeout(tick,1e3)') – Oriol
你怎麼知道畫布沒有顯示? –