我创建一个纸牌游戏,一切都很好,但我不能够按照以下步骤创建卡片堆栈一个纸牌叠一叠卡片:创建的类似于AS3
如果我有4张牌被代表在阵列黑桃如4,5,6,7-然后我想用最少的优先级的卡被放置在顶部和放在后面的第一个最高优先级的卡。
如何才能做到这一点?
我创建一个纸牌游戏,一切都很好,但我不能够按照以下步骤创建卡片堆栈一个纸牌叠一叠卡片:创建的类似于AS3
如果我有4张牌被代表在阵列黑桃如4,5,6,7-然后我想用最少的优先级的卡被放置在顶部和放在后面的第一个最高优先级的卡。
如何才能做到这一点?
我假设你知道如何创建的DisplayObject(你的视觉卡),所以我就不提了,在我的答案。
我会创造一个支架精灵,并添加我的卡到拿到过卡的深度顺序控制。在您添加的卡堆精灵或者通过使用addChild(添加displayObject的前面)或addChildAt(添加displayObject的在你想要的位置)。如果您使用addChildAt并使用0作为索引,它会将其添加到所有其他displayObjects下面并向上推索引。如果您已在显示列表中拥有卡片,则可以使用setChildIndex更改索引。
var cardList : Array;
var cardStack : Sprite = new Sprite();
addChild(cardStack);
for(var i : int = 0 ; i < cardList.length ; i++)
{
// adds it at below all displayObject in "cardStack"
cardStack.addChildAt(cardList[i], 0);
// adds it on top of all displayObject in "cardStack"
cardStack.addChild(cardList[i]);
}
然后你需要更具体的问题是什么。 – Mattias
最简单的方法是使用addChildAt(...)使用removeChildAt(...)的舞台中删除所有的牌,然后再添加它们(以正确的顺序)。
Usign马蒂亚斯例如:
var sortedCardArray:Array = [];
while (cardContainer.numChildren)
{
sortedCardArray.push(cardContainer.removeChildAt(0));
}
/*
/ This assumes that all the values of the cards
/ are numeric (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack=11, Queen=12, King=13, Ace=14)
*/
sortedCardArray.sortOn("variableWithTheCardNumber", Array.NUMERIC | Array.DESCENDING);
var n:int = sortedCardArray.length;
for(var i:int = 0 ; i < n ; i++)
{
cardContainer.addChild(sortedCardArray[i]);
}
这是关于排序,你要排序算法? – rptwsthi
已经实现了数组的排序,现在只想知道如何在舞台上放置卡片。 –
你数组包含什么,对显示对象,字符串的引用?也许你会得到你认为有用的答案,如果你提供了关于你想要做什么的更多信息,也许有些代码? – shanethehat