我仍在爲簡單的JavaScript遊戲而苦苦掙扎。這是我以前的問題:Simple javascript game, hide/show random square如何在不同的時間隱藏/顯示多個廣場
某些廣場隨機顯示和隱藏幾秒鐘,你必須點擊它們。我使用RaphaelJS來繪製廣場和幾個JQuery($ .each()函數)
我無法使用setInterval爲每個方塊工作的hide/show。通過Mishelle提出的功能看起來不錯,但我得到了「這不是一個函數」的錯誤。我已經測試不同的東西,但我認爲它不是那麼明顯:/
window.onload = function() {
var paper = new Raphael(document.getElementById('canvas_container'), 500, 500);
// random function to for the x and y axis of the square
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var rectanglemain = paper.rect(0, 0, 500, 500).attr({fill: "white",});
var win_click = 0; // set the var to count the click on the square
var recs = [];
for(var i = 0; i < 50; i++) {
var x = random(1,450);
var y = random(1,450);
var rec = paper.rect(x, y, 50, 50).attr({fill: 'blue'});
recs.push(rec); // add the square in an array
recs[i].click(function() {
//counting the clicks
win_click = win_click + 1;
})
function hideSquare() {recs[i].hide();}
hideSquare();
}
rectanglemain.click(function() {
alert('you click on ' + win_click + ' squares');
});
/* here is a function made by Mishelle on my previous question, unfortunately I can't make it work (this is not a function error).
function showBriefly (timeFromNow, duration) {
window.setTimeout(this.rec.show, timeFromNow);
window.setTimeout(this.rec.hide, timeFromNow + duration);
}
recs[2].showBriefly(1000, 3000); to test the function
*/
}
感謝您的幫助:)
謝謝,我不得不刪除「這個」。它只顯示(第一個?)方塊。我嘗試與recs [我],但它不承認,但recs [2]的作品。如果添加單詞「調試器」,則爲 – Olivier 2011-05-08 16:57:55
(沒有引號)settimeout並運行firefox中的代碼與螢火蟲擴展功能打開,你會看到什麼* this *是在指定的時間 – AndreasKnudsen 2011-05-08 16:58:48
謝謝你的技巧,_this_引用「undefined」:/但它看起來像我越來越接近解決方案。 – Olivier 2011-05-08 17:17:47