Reputation: 1
So I have a 3x3 grid of text fields that are meant to show to the 3 best score along with the lap times and carHealths that have been achieved since the game was launched and if a better score then one of the ones in the current leader board is made then the new score, lap time and carHealth replace the old one and move everything below it down by one.
The problem is it only replace the top score even if it is a worse score if just leaves the other 2 spots untouched. Am I just missing something very obvious or am i going about this all wrong?
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);
}
}
Upvotes: 0
Views: 54
Reputation: 1806
Oh god :) What do you do if you suddenly want to display top 10 scores ?
How about this approach: You store all information of your cars in ONE object (or class) and then sort your array by score. This way you dan't have to mess with three separate arrays (and maybe you will want to add more properties to your car later):
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);
}
}
Upvotes: 1