1
import fl.events.*;  

const PointsStart:int=0;  

var Points:int=PointsStart;  

youChose.text=String(Points)+" points";  

comboBox.prompt='Contestants'  
comboBox.addItem({label:"John Smith",Points:10});  
comboBox.addItem({label:"Chris Tucker",Points:12});  
comboBox.addItem({label:"Paul Allen",Points:14});  

comboBox.addEventListener(Event.CHANGE, listevalg);  


function listevalg (evt:Event)  
{  
    Points=comboBox.selectedItem.Points;  
    youChose.text=String(Value)+" points";  
}  

我想要一个文本框来说明当前的排名。假设我在组合框中选择 Chris Tucker,我希望文本框显示“他在第二位”之类的内容

4

1 回答 1

1

首先,您需要知道每个人的排名。为此,您可以复制人员列表并按点数对其进行排序,然后将他们的排名注册为他们在该列表中的位置。

import fl.events.*;  

const PointsStart:int=0;  

var Points:int=PointsStart;  

youChose.text=String(Points)+" points";  

comboBox.prompt='Contestants'  

var people:Array = [{label:"John Smith",Points:10},
                    {label:"Chris Tucker",Points:12},
                    {label:"Paul Allen",Points:14}];

// Copy of "people", sorted by Points, descending.
var sortedPeople:Array = people.concat().sortOn("Points", Array.DESCENDING | Array.NUMERIC);

for each (var person:Object in people) {
    // Register this person's ranking as its position in the sorted array + 1
    // (because indexes start at 0)
    person.Rank = sortedPeople.indexOf(person) + 1;
    comboBox.addItem(person);
}

comboBox.addEventListener(Event.CHANGE, listevalg);  


function listevalg (evt:Event)  
{  
    Points=comboBox.selectedItem.Points;
    // The ranking is now available as "item.Rank".
    youChose.text=String(Points)+" points, rank "+String(comboBox.selectedItem.Rank);
}

如果您实际上不需要将当前顺序保留在组合框中,则甚至不需要在排序之前复制数组。

于 2012-11-21T20:25:26.217 回答