2012-02-10 45 views
0

如何從陣列中刪除任何對象notafications其中​​?修剪陣列中的對象,其中「I.active = false」

澄清: 我基本上試圖讓數組中的某個對象「標記自己刪除」,一旦它執行它所需要的。

我推參數與此代碼的數組:

notafications.push(notafication({ 
    font: "60px pong", 
    baseline: "top", 
    align: "left", 
    color: "#FFFFFF", 
    text: "this is a test", 
    x: 100, 
    y: 100 
})); 

然後這個函數的邏輯巫術吧:

function notafication(I) { 
I.active = true; 
I.render = true; 
I.count = 3; 
I.flashTimer = setInterval(function() { 
    if (!pingpong.paused) { 
     I.count--; 
     if (I.count%2 == 1) { 
      I.render = true; 
     } 
     else { 
      I.render = false; 
     } 
     if (I.count == 0) { 
      clearInterval(I.flashTimer); 
      I.render = false; 
      I.active = false; 
     } 
    } 
},750); 
return I; 
} 

最後,這裏是通知渲染的部分我gameloop。

if (render.notafication) { 
    notafications.forEach(function notafication(I) { 
     ctx.font = I.font; 
     ctx.textAlign = I.align; 
     ctx.textBaseline = I.baseline; 
     ctx.fillStyle = I.color; 
     if (I.render) { 
      ctx.fillText(I.text,I.x,I.y); 
     } 
    }); 
} 

注: 是的,我知道我必須通知拼錯了我的代碼。直到代碼的這部分完成後,我才繼續拼寫錯誤。然後我會發現 - 將其替換爲正確的拼寫。

回答

0

這麼多需要幫助。我想出瞭解決方案。

我只是簡單地添加這段代碼到我的gameloop:

notafications = notafications.filter(function notafication(I) { 
    return I.active; 
}); 

希望這將是使用的人的未來。

+1

Aauuugghhhhh請修復現在的拼寫,這是毀了我的心! – ErikE 2012-02-10 07:13:59

+0

哈哈,別擔心,我在上面。 – 2012-02-10 15:02:15