如何在actionscript 3.0中对钻石,梅花,黑桃,心等卡片进行排序
Posted
技术标签:
【中文标题】如何在actionscript 3.0中对钻石,梅花,黑桃,心等卡片进行排序【英文标题】:how to sort cards like diamond, club, spade,heart in actionscript 3.0 【发布时间】:2017-10-25 17:13:03 【问题描述】:我有数组中的卡片,并且我有排序按钮,但我不知道如何进行排序,例如钻石、梅花、黑桃、心形卡片想要与这张卡片分开..
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,
];
谁知道,请详细说明一下。谢谢
【问题讨论】:
【参考方案1】:我建议添加一些额外的参数,比如“重量”:
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;
);
【讨论】:
【参考方案2】:您可以像@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。
【讨论】:
以上是关于如何在actionscript 3.0中对钻石,梅花,黑桃,心等卡片进行排序的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Actionscript 3.0 中获取数组的所有排列?
在 ActionScript (3.0) 中干净地合并两个数组?