Graphics Reference
In-Depth Information
exampleinalittlebit).Afterwehavethetemporarycard,let
sgo
ahead and use it to create a new Bitmap called
_frontSkin
,which
we
'
ll use as the face of the card. The next thing we need to do is
check to see if an image is available for the back of the card. If it is,
we
'
lltaketheappropriatestepstouseittocreateanotherBitmap
called
_backSkin
(the back of the card).
Now that we have something to use for both the face and the
back of the card, the next thing we
'
re going to do is create a new
Sprite called
cardSprite
.Oncethat
'
s instantiated, we add the
_frontSkin
and
_backSkin
, set the location of the card (
cardSprite.x
and
cardSprite.y
), and set its
buttonMode
to true, so the hand cursor
will show up giving the users a visual clue that they can click on the
card. Finally, we add a click listener to fire the
showCard
method and
add the card to the stage with
addChild(cardSprite)
.
Now let
'
s take a look at this little interaction method named
showCard
. Before I explain this one, let me point out that while there
aren
'
'
t any animated transitions happening during the card flip, it
wouldn
'
t be hard for you to add one of your choosing. I thought if
I left it out, you would have more freedom to play and experiment
with things like animating the card flip or fading from the back to
the front. Now that we have that settled, let
s take a look over this
method. The very first thing that happens is that we check to see if
the
_allowClick
variable is true or false. If it
'
'
sfalse,then
return
kicks the user out of the method. However, if it
s true, then we jump
right in to turn off the
buttonMode
, so the user can
'
t click on it again
and hide the back of the card to reveal the front (this is where you
would want to insert a transition). Next, we add the card we just
clicked to our
_cardsToCompare
Array. After the user clicks on a sec-
ond card, the
_cardsToCompare
Array has a length of two, and we can
check those two cards to see if they match by using the
BitmapData
.compare
method. If the cards match, we dispatch the
MemoryGame
Event.CORRECT_MATCH
event and call the
correctMatch
method. If
they don
'
t, we call the
MemoryGameEvent.INCORRECT_MATCH
event, set
_allowClick
to false so the user can
'
t keep clicking cards, and then
run a
setTimeout
to give the user time (1 s in this case) to realize he
or she was wrong before calling the
hideCards
method.
'
TIP
The
compare
method of BitmapData compares the width, height, and pixel
values within two BitmapData objects. If all three of those items are
equivalent, the
compare
method returns 0. Check out the Adobe LiveDocs
for more information.
ll breeze
through them real quick. First, the
hideCards
method does exactly
what it says by hiding the cards (or turning them over). It takes the
The two last methods in this engine are pretty short, so I
'