Create a stack of cards similar to the Solitaire stack in as3

I create a card game, everything is in order, but I can not create a stack of cards as follows:

If I have 4 cards that are presented in an array, such as 4,5,6,7 of Spades, then I want the card with the lowest priority to be placed on top, and the card with the highest priority to be placed behind the first.

How can this be achieved?

+4
source share
2 answers

I assume that you know how to create DisplayObjects (your visual maps), so I will not mention this in my answer.

I would create an owner sprite and add my cards to it to gain control over the order of the depth of the map. You add cards to the stack sprite using either addChild (adds displayObject in front) or addChildAt (adds displayObject to the desired position). If you use addChildAt and use 0 as your index, it will add it below all other displayObjects and push one index up. If you already have maps in the display list, you can change the index using 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]); } 
+1
source

The easiest way is to remove all cards from the scene using removeChildAt (...) and add them again (in the correct order) using addChildAt (...).

Usage Mattias example:

 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]); } 
+1
source

All Articles