2014-11-23 85 views
0

我正在製作一個簡單的HTML5遊戲,我試圖實現一個成就係統。我的方法是創建一個具有名稱,描述,條件等屬性的成就對象...所以現在我需要做的就是通過調用類似於:Achievement.LookARock = new Achievement("Look! A Rock!", "Mined stone for the first time!", (stone.count == 1), false);來創建不同的成就對象。所以現在我所要做的就是檢查成就條件是否成立。我通過調用setInterval來做到這一點。這裏是我使用的代碼:布爾作爲參數/參數?

//Achievements 
Achievement.LookARock = new Achievement("Look! A rock!", "Mined stone for the first time.", (stone.count >= 1), false); 
Achievement.IGotWood = new Achievement("Haha... I got wood...", "Mined wood for the first time.", (wood.count == 1), false); 
Achievement.Stone100 = new Achievement("Stone 100", "Mined 100 stone blocks.", (stone.count == 100), false); 
Achievement.Stone500 = new Achievement("Stone 500", "Mined 500 stone blocks.", (stone.count == 500), false); 
Achievement.Stone1000 = new Achievement("Stone 1000", "Mined 1000 stone blocks.", (stone.count == 1000), false); 
Achievement.FirstCraft = new Achievement("First Craft", "Crafted your first item.", (craftedtools.length == 2), false); 
Achievement.Shiny = new Achievement("Oooooo... shiny!", "Mined a diamond!", (diamond_ore.count == 1), false); 
Achievement.ICantButter = new Achievement("I can't believe it's not butter!", "Mined gold!", (gold_ore.count == 1), false); 
Achievement.IronMan = new Achievement("I am Iron Man...", "Mined metal!", (metal_ore == 1), false); 
//Achievement.Level1 = new Achievement("Level 1 Completed!", "Crafted a Diamond Pickaxe!", craftedtools.includes(diamond_pick), false); 

/*var achievements = [Achievement.LookARock, 
        Achievement.IGotWood, 
        Achievement.Stone100, 
        Achievement.Stone500, 
        Achievement.Stone1000, 
        Achievement.FirstCraft, 
        Achievement.Shiny, 
        Achievement.ICantButter, 
        Achievement.IronMan];*/ 

function checkAchievement(achievement){ 

    var condition = achievement.condition; 
    var achieved = achievement.achieved; 

    if(condition !== false){ 

     return true; 

    } 

    return false; 

} 

function checkAchievements(){ 

    for(var i = 0; i < achievements.length; i++){ 

     var a = achievements[i]; 

     if(checkAchievement(a)){ 

      alert(a.name + "\n" + a.description); 

     } 

    } 

} 

function update(){ 

    try{ 

     checkAchievements(); 

    }catch(e){ 

     console.log(e); 

    } 

} 

changeTool(stone_pick); 
makeMineGenerator(); 

setInterval(update, 50); 

我知道它確實是檢查成就。但即使計數是正確的,該條件總是返回錯誤。有什麼辦法可以解決這個問題嗎?

+1

什麼「數」?您發佈的代碼中發生的所有事情都是您檢查布爾標誌的值。 – Pointy 2014-11-23 21:08:49

回答

0

你創造成就的方式是有原因的。 (stone.count == 1)在對成就構造函數的調用之前進行評估,以便在創建對象時condition屬性被分配爲false

你需要建立你成就對象與函數調用,當你想檢查的成就。

Achievement.LookARock = new Achievement(
    "Look! A Rock!", 
    "Mined stone for the first time!", 
    function() { 
    return stone.count == 1; 
    }, 
    false 
); 

然後檢查成就,您可以使用if (achievement.condition())

+0

非常感謝!這解決了我的問題。 – arjay07 2014-11-23 23:44:14