Step 4: Put it all together
Now we've got our dealer, and we've got our playing card. All we have to do is finish up some code on the playing card, and deal them suckers out! O and of course, make our beginning and ending scenes.
Preparing the cards to turn:
For the cards, take and instance playingCard and put it on the main stage. Make sure it's low enough to be off screen. Name this instance pCard. Not intentially named after the captain of the Enterprise, but we'll work with it. Place the following code:
on (release) { // only animate 2 unique cards at any given time. if ((_root.cardsInPlay < 2) && (this.theCard._currentframe <= 1)) { // if this is the first card then save picture number //*** NOT INDENTED FOR WEBPAGE PURPOSES *** if (_root.cardsInPlay == 1) { _root.card2Match = this.theCard.cardFace._currentframe; } // begin animating this.theCard.play(); // count attempt _root.attemptCount++; _root.gameAttempts = "ATTEMPTS: " + Math.round(_root.attemptCount / 2); } }
This simple code tells the card, that when it is released to check 2 things
1) if 2 cards are already in play
2) if it is not already animating
If it passes both, then it continues on to check if it's the first card in play or the second. Depending on the answer it sets the card to match.
Now, animate the card with the play directive and we increment the attempt count. Remember? Attempt count is going to be used to calculate the score.
Seems like the cards are ready to turn. Let's do our simple dealing code.
Dealing the cards:
On the main stage, in our actions layer and probably under the functions, lets set the cards to deal:
// create a deck fullDeck = new Array(); fullDeck = createDeck(); // Total unique cards totalUniqueCards = 16; // Total number of cards in play totalCardsNum = totalUniqueCards * 2; // deal the cards + duplicates from the deck deal(totalUniqueCards, fullDeck); // wait here now. stop();
Simple enough? Thanks functions!!! Now we can play memory, set the number of cards to deal, set the pictures easily and manipulate our cards easily. This game could very easily be built up to do more advanced features.
» Level Intermediate |
Added: 2005-04-06 Rating: 4.43 Votes: 44 |
» Author |
I'm the admin for BEF |
» Download |
Download the files used in this tutorial. |
Download (115 kb) |
» Forums |
More help? Search our boards for quick answers! |