如何在动作3.0中对钻石,俱乐部,铁锹,心脏等卡片进行排序

时间:2017-05-25 06:42:00

标签: arrays actionscript-3 actionscript flex3 actionscript-2

我有阵列牌,我有排序按钮,但我不知道如何做那种像钻石,俱乐部,铁锹,心卡想要从这张卡分开..

    var aList:Array =
            [
                {card:Globe.self.realstage.joker_mc, x:605.55, y:195.45},
                {card:Globe.self.realstage.king_mc,  x:323.80, y:298.45},
                {card:Globe.self.realstage.queen_mc, x:45.85, y:213.95},
                {card:Globe.self.realstage.a_mc,     x:605.55, y:195.45},
                {card:Globe.self.realstage.ten_mc,   x:323.80, y:298.45},
                {card:Globe.self.realstage.five_mc,  x:45.85, y:213.95},
                {card:Globe.self.realstage.two_mc,   x:605.55, y:195.45},
                {card:Globe.self.realstage.nine_mc,  x:323.80, y:298.45},
                {card:Globe.self.realstage.four_mc,  x:45.85, y:213.95},
            ];

任何人都知道你能详细说明这个。谢谢你

2 个答案:

答案 0 :(得分:3)

我建议添加一些额外的参数,比如说“重量”:

 var aList:Array =
       [
           {card:Globe.self.realstage.joker_mc, x:605.55, y:195.45, weight: 11},
           {card:Globe.self.realstage.king_mc,  x:323.80, y:298.45, weight: 13},
           {card:Globe.self.realstage.queen_mc, x:45.85, y:213.95, weight: 12},
           {card:Globe.self.realstage.a_mc,     x:605.55, y:195.45, weight: 14},
           {card:Globe.self.realstage.ten_mc,   x:323.80, y:298.45, weight: 10},
           {card:Globe.self.realstage.five_mc,  x:45.85, y:213.95, weight: 5},
           {card:Globe.self.realstage.two_mc,   x:605.55, y:195.45, weight: 2},
           {card:Globe.self.realstage.nine_mc,  x:323.80, y:298.45, weight: 9},
           {card:Globe.self.realstage.four_mc,  x:45.85, y:213.95, weight: 4},
       ];

然后根据此权重对数组进行排序:

// in descending order
aList.sort(function (c1:Object, c2:Object):int
        {
            if (c1.weight > c2.weight) return -1;
            if (c1.weight < c2.weight) return 1;
            return 0;
        });

// in ascending order:
aList.sort(function (c1:Object, c2:Object):int
        {
            if (c1.weight > c2.weight) return 1;
            if (c1.weight < c2.weight) return -1;
            return 0;
        });

如果你不能改变对象(或者由于某种原因你不想在那里增加重量),你可以创建一个外部辅助函数:

// somewhere 
function getWeight(data: Object):int {
    switch(data.card) {
        case Globe.self.realstage.two_mc:
            return 2;
        case Globe.self.realstage.four_mc:
            return 4;

        ...

        default: return 0;
    }
}

aList.sort(function (c1:Object, c2:Object):int
    {
        if (getWeight(c1) > getWeight(c2)) return 1;
        if (getWeight(c1) < getWeight(c2)) return -1;
        return 0;
    });

答案 1 :(得分:0)

你可以像@Nbooo那样添加额外的参数,并像这样使用SortSortField

var sortField : SortField = new SortField();
sortField.name = "weight";
sortField.numeric = true;

var sort: Sort = new Sort();
sort.fields = [sortField];

this.aList.sort = sort;
this.aList.refresh();

参考here