2017-08-31 34 views
0

因此,我有一個3x3的文本字段網格,旨在顯示3個最佳分數以及自從遊戲啓動以來獲得的單圈時間和carHealths,並且如果獲得更好的分數,現在的排行榜中的成績就是新的成績,圈速和carHealth取代了原來的成績,並將其下的所有成績都降低了一分。存儲排名前3的分數

問題是它只是取代最高分,即使它是一個更糟糕的分數,如果只剩下其他2個點不變。我是否錯過了一些非常明顯的東西,或者我是否會發現這一切都是錯的?

function leaderBoard(): void 
{ 
    if (score < scoreArray[2]) 
    { 
     return 
    } 

    if (score > scoreArray[0]) 
    { 
     scoreArray.unshift(score); 
     lapTimerArray.unshift(lapTimer.currentCount); 
     carHealthArray.unshift(carHealth); 

     scoreArray.pop() 
     lapTimerArray.pop() 
     carHealthArray.pop() 
    } 
    else if (score > scoreArray[1]) 
    { 
     scoreArray.splice(1, 0, score); 
     lapTimerArray.splice(1, 0, lapTimer.currentCount); 
     carHealthArray.splice(1, 0, carHealth); 

     scoreArray.pop(); 
     lapTimerArray.pop(); 
     carHealthArray.pop(); 
    } 
    else if (score > scoreArray[2]) 
    { 
     scoreArray.pop(); 
     lapTimerArray.pop(); 
     carHealthArray.pop(); 

     scoreArray.append(score); 
     lapTimerArray.append(lapTimer.currentCount); 
     carHealthArray.append(carHealth); 
    } 
} 
+0

如果我正確理解你的問題,也許問題從你的使用率莖'else if',而不是單獨做'if'。目前,如果您的'scorearray'中的第一項小於'score',則您執行代碼。其他條款只有在條件失敗時才能被觸發,所以只有在score scoreArray [1]。 – DodgerThud

+0

所以我試圖讓他們所有如果陳述,但隨後所有三行顯示相同的分數,並在同一時間被取代。我也嘗試在每個if語句的底部添加回車,但是隻有第一個是替換,其他的不管分數如何都不會改變。 – Jester

回答

1

哦,上帝:)如果你突然想要顯示前10的分數,你會怎麼做?

這種方法如何:您將汽車的所有信息存儲在一個對象(或類)中,然後按分數對您的數組進行排序。這樣,您dan't得一塌糊塗三個單獨的數組(也許你會希望以後更多的屬性添加到您的車):

var car1:Object = {name:"Car 1", score:100, lapTimer:100, carhealth:50}; 
var car2:Object = {name:"Car 2", score:1050, lapTimer:100, carhealth:50}; 
var car3:Object = {name:"Car 3", score:700, lapTimer:100, carhealth:50}; 

var myCars:Array = [car1, car2, car3]; 

// Then you probably want to pass your car objects to your cars and modify them from there: car3.score = 400 etc. The car objects can be created dynamically based on how many cars you want 

// In the end 
function displayScores():void 
{ 
    myCars.sortOn("score"); // sort cars on score property 

    // display top 3 
    for(i:int = 0, i < 3; i++) 
    { 
     trace("Place " + (i+1) + " - " + myCars[i].name + ", Score " + myCars[i].score); 
    } 
}