Graphics Reference
In-Depth Information
two cards in the
_cardsToCompare
Array, covers the face of the card
with the back of the card (another opportunity for a transition here),
and makes the card interactive again by setting its
buttonMode
to true.
After that, we just empty the
_cardsToCompare
Array with
splice(0)
and return
_allowClick
totrue.Finally,wehavethe
correctMatch
method, which we fired earlier when there was what? Yes, a correct
match. The first thing this method does is increment
_matches
Complete
. After that, it clears out the
_cardsToCompare
Array and
checks to see if the number of cards matched is equal to the original
number of cards in the deck. If it is equal, a
MemoryGameEvent.GAME_
COMPLETE
event is dispatched, and that
'
s all there is to it.
Sample Use of MemoryGame
Within the com.flashadbook.base package, you can find this sample
code in MemoryGameSample.as, and here
'
s the quick breakdown of
what
s happening within that class. First, there are several variables
that get set up.
_memoryGame
is an instance of MemoryGame, and
_gameCards
is an array of the Linkage names for some images that are
in the library of the .fla. These are the card faces. Next up are some
variables that hold the coordinates for the rows and columns that
we
'
ll use those coordinates to fill the
_coordArr
Array with x and y values. Next in line is the constructor
method, which, at this point, does nothing more than fire off the
new-
Game
method and passes in
_gameCards
for the required
cardsToUse
parameter and
_coordArr
for the optional
locationArr
parameter.
The result of this sample is a game like the one in Figure 13.2.
'
ll use to place our cards. We
'
TIP
Note that I populated the
_coordArr
with Objects containing x and y
values, but you could just as easily populate it with Points.
s take a quick look at the
newGame
method. This one
is another very straightforward method that instantiates a new
MemoryGame with the array of cards (
cardsToUse
), and if you have
one to use, the Linkage name of the image in the library that will be
thebackofthecard.Afterthegameisinstantiated,it
Now let
'
s added to the
stage, and the cards are dealt with the
MemoryGame.deal
method. In
this case, we
'
re using the custom layout and passing in that array of
coordinates we created earlier. The last thing we do in this method is
to add listeners to the game for each of the MemoryGameEvent
events. The last three methods in this class are simply the handlers
for each of those listeners.
'
package com.flashadbook.base {
import flash.display.MovieClip;
import flash.events.MouseEvent;