如何在 actionscript 3.0 中对方块、梅花、黑桃、红桃等卡片进行排序
how to sort cards like diamond, club, spade,heart in actionscript 3.0
我有阵列中的卡片,我有排序按钮,但我不知道如何进行排序,例如方块、梅花、黑桃、红心卡片想要与这些卡片分开..
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},
];
任何人都知道,你能详细说明一下吗one.Thank你
我建议添加一些额外的参数,比如 "weight":
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;
});
您可以像@Nbooo 说的那样添加额外的参数,并像这样使用 Sort
和 SortField
:
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.
我有阵列中的卡片,我有排序按钮,但我不知道如何进行排序,例如方块、梅花、黑桃、红心卡片想要与这些卡片分开..
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},
];
任何人都知道,你能详细说明一下吗one.Thank你
我建议添加一些额外的参数,比如 "weight":
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;
});
您可以像@Nbooo 说的那样添加额外的参数,并像这样使用 Sort
和 SortField
:
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.