我想知道是否有可能在一個不復雜的方式使這個循環?For循環一個畫布動畫
我正在使用9種不同顏色的代碼...... for-loop會縮短我的代碼量。我對JS很新穎,我不能真正想到一種簡單的方式。唯一改變的是「紅色」到「綠色」或「黃色」等等。 顏色名稱是唯一的變化,'x'和'y'座標爲。
} else if (collides(myRectRed, x, y)) {
context.fillStyle = "White";
context.fillRect(107, 7, 90, 110);
function callbackRed() {
setTimeout(function returnSizeRed() {
context.fillStyle = "Red";
context.fillRect(107, 7, 90, 110);
},50);
}
function callMIDRed(){
setTimeout(function displayMidRed(){
context.fillStyle = "Red";
context.fillRect(119, 22, 60, 80);
return callbackRed();
},50);
}
setTimeout(function displayRectRed() {
context.fillStyle = "Red";
context.fillRect(139, 42, 20, 40);
return callMIDRed();
},50);
你會想看看* *關閉,然後你可以輕鬆使用循環。 – Bergi 2014-09-25 15:52:03