Developing An Application in Flash And AS3 Part Three
Wednesday, 8 July 2009
Welcome to the third part of this development project. You can catch up with the two previous posts using the links at the bottom of this page.
Update So Far
Progress has been a little slower than I would have liked as I haven’t had much time over the last few evenings to work on the project. However, the game is now at least playable – check out the end of this post for a playable progress so far beta version. I have been working on two issues with the pairs game which have taken a little time to resolve.
The first is object management, I noticed too many object instances were being created (e.g. starting a new game, randomly sorting the cards) so I have restructured much of the code to keep the number of objects to a minimum – this also makes the code flow much better. For the cards, I now create the twenty instances just once at the beginning of the game and use object variables to handle any particular functional requirements.
Secondly, now the basic game framework is up and running I have been working through some of the core logic of the pairs game trying to figure out some of the best ways of handling the mechanics. This goes as follows
- At the outset the player can click on any one card – once clicked the engine needs to remember this card
- If the player attempts to click on the same card again – return it to its face down position and forget this card
- If a player clicks on a second (different) card (if there is already one remembered card and it is different) we need to remember the second card and take some action
- If the two cards match remove them both, increase the match counter, and forget about them
- If all cards are turned (match counter reaches 10) then end the game
- If the two cards are different return them to their face down position and forget about them
- If the two cards match remove them both, increase the match counter, and forget about them
Regarding the last point – “if the two cards are different return them to their face down position and forget about them” needs a little extra thought. This is because we don’t want to return them to their face down position immediately – there needs to be a short pause (around 2 seconds) for the player to get a chance to remember them. I have achieved this by creating a Timer in the Card object but now we need to make some decisions:
Can the player click on another card during this pause or do they have to wait?
The ideal scenario would be to allow the player to continue to play. However, this opens up a few other questions:
- What happens if the player clicks on one of these remembered cards again during the pause?
- What happens if the player clicks on a different card during the pause?
The preference here would be to return the two cards instantly to their face down position if the player clicks on another card – allowing the player an opportunity to shave a second or two off their time. However, this means we cannot forget about the two cards immediately but instead we need to add a check if the player clicks on a card whether there are already two remembered cards with a timer running and if so cancel the timers and immediately forget about the two cards.
The flowchart for this logic follows:
The current action script 3 code for this logic is as follows:
var aCard:Card = event.target as Card;
// if player attempts to click on another card whilst they are both turned
if ( iCard2 != null )
{
// face down
iCard1.doneTimer();
iCard2.doneTimer();
// forget about them both
iCard1 = null;
iCard2 = null;
}
// if no cards are turned
if ( iCard1 == null )
{
iCard1 = aCard; // remember the card
iCard1.swapImage(); // put the card in the face up position
iCount++; // increase the play counter
}
// if the first card is already turned
else
{
// if the two cards are the same
if ( iCard1 == aCard )
{
iCard1.resetImage(); // face down the first card
iCard1 = null; // forget about the first card
iCount++; // increase the play counter
}
else
{
// if the two cards are different
iCard2 = aCard; // remember the card
iCard2.swapImage(); // put the second card in the face up position
iCount++; // increase the play counter
// if both cards face value match
if ( iCard1.getCardValue() == iCard2.getCardValue() )
{
// remove the cards from the stage
stage.removeChild(iCard1);
stage.removeChild(iCard2);
// forget about the cards
iCard1 = null;
iCard2 = null;
iScore++; // increase the score
iFound++; // increase the pair count
// all pairs found? if so, end the game
if ( iFound == 10 ) { endGame(); }
}
// if both cards face value differ
else
{
// start the timer to return them face down
iCard1.TimeDown();
iCard2.TimeDown();
}
}
}
The First Beta Release
This should at least be playable – just remember it isn’t finished and there is still a bit of logic to figure out. If you spot anything please let me know in comments.
Catch Up Reading
Developing An Application In Flash And AS3 Part Two
Developing An Application Using Flash and Action Script 3

Jason Slater is an independent technologist and blogger.