如何将数组中的前 3 个分数放入 3 个不同的文本字段,并将它们按最高分到最低分排序?我的代码:
var top3Array:Array = [];
var totalScore = int((numeratorCounter/denominatorCounter)*100)/100;
fucntion scoreBoard(): void{
top3Array.sortOn("totalScore");
for(i:int = 0, i < 3; i++)
{
count1.text = String(count);
score1.text = String(top3Array[0]);
}
}
你的问题有点不清楚,但听起来你想将所有分数添加到一个数组中,取前 3 个分数并将其显示在一些文本字段中?
正如 @Organis 提到的,数组的内容确实决定了您需要进行哪种排序。
如果分数数组仅包含数字列表,那么您只需要使用 .sort() 函数而不是 .sortOn()
假设每次玩家死亡时,每个分数都会被推送到名为 allScores 的数组中,您可以使用与此类似的代码:
const allScores:Array = [24, 10, 16, 28, 5, 29];
// Sort scores into descending numeric order.
const sortedScores:Array = allScores.sort(Array.DESCENDING | Array.NUMERIC);
// >> 29,28,24,16,10,5
// Get the first 3 elements of the array - i.e. the 3 highest numbers.
const top3scores:Array = sortedScores.slice(0, 3);
// >> 29,28,24
// Set the text fields' contents to the top scores.
score1.text = top3scores[0];
score2.text = top3scores[1];
score3.text = top3scores[2];