topical media & game development
game-flex-memory-game.mx
game-flex-memory-game.mx
[swf]
flex
<?xml version="1.0"?>
<!-- Minigame Memory -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="cardImageService.send()">
<mx:HTTPService
id="cardImageService"
url="game-flex-memory-cards.xml"
resultFormat="e4x"
result="cardImageResultHandler(event);"
fault="cardImageFaultHandler(event);"
/>
<mx:Panel id="titlePanel" title="Memory Game" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.*;
import mx.events.*;
private var defaultPath:String = "../assets/images/memory/";
[Bindable]
private var gameArray:Array;
private var toolTipArray:Array;
private var oneCardFlipped:Boolean;
private var currentCard:String;
private var previousCard:String;
private var currentCardIndex:int;
private var previousCardIndex:int;
private var cardPairsTaken:int;
private var timerActive:Boolean;
private var genericCardSide:String;
private var assetsPath:String;
private var playerScore:int;
private var totalClicks:int;
private var cardImageFeed:XML;
private var myMenu:Menu;
private var myXMLDataArray:Array = ["game-flex-memory-cards.xml","game-flex-memory-cards2.xml"];
private function initMemoryGame():void {
playerScore = 0;
cardPairsTaken = 0;
oneCardFlipped = false;
currentCard = "";
previousCard = "";
currentCardIndex = -1;
previousCardIndex = -1;
timerActive = false;
totalClicks = 0;
setScore();
setTotalClicks();
fillArray();
}
private function fillArray():void {
gameArray = new Array(cardImageFeed.card.length()*2);//We need two of each pictures
toolTipArray = new Array(gameArray.length);
for (var i:int = 0; i < gameArray.length; i++ ){
gameArray[i] = assetsPath + cardImageFeed.card[0].url;
toolTipArray[i] = cardImageFeed.card[0].name;
}
var random:int;
var j:int;
for (var k:int = 1; k < cardImageFeed.card.length(); k++){
j = 0;
while (j < 2){
random = Math.random()*gameArray.length;
if (gameArray[random] == assetsPath + cardImageFeed.card[0].url){
gameArray[random] = assetsPath + cardImageFeed.card[k].url;
toolTipArray[random] = cardImageFeed.card[k].name;
j++;
}
}
}
}
private function reveal(cardIndex:int):void {
if (timerActive) return;
currentCardIndex = cardIndex;
currentCard = card[currentCardIndex].source = gameArray[currentCardIndex];
card[currentCardIndex].toolTip = toolTipArray[currentCardIndex];
if (currentCardIndex == previousCardIndex) return;//Clicking on the already turned card
totalClicks++;
setTotalClicks();
if (!oneCardFlipped){
previousCardIndex = currentCardIndex;
previousCard = currentCard;
oneCardFlipped = true;
}else {
var timer:Timer = new Timer(800,1);
timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
timerActive = true;
timer.start();
}
}
private function dispatchComplete(event:TimerEvent):void {
if (!timerActive) return;
if (currentCard == previousCard){
card[previousCardIndex].source = "";//Remove cards from play
card[currentCardIndex].source = "";
playerScore++;
cardPairsTaken++;
setScore();
}else {
card[previousCardIndex].source = card[currentCardIndex].source = genericCardSide;
}
card[previousCardIndex].toolTip = card[currentCardIndex].toolTip = "";
previousCardIndex = -1;
previousCard = "";
oneCardFlipped = false;
timerActive = false;
}
private function setGenericCardSide():String {
return genericCardSide;
}
private function setScore():void {
scoreLabel.text = "Score: " + playerScore;
}
private function setTotalClicks():void {
totalClicksLabel.text = "Total clicks: " + totalClicks;
}
private function cardImageResultHandler(event:ResultEvent):void {
cardImageFeed = event.result as XML;
if (cardImageFeed.@id != null) titlePanel.title="Memory Game - " + cardImageFeed.@id;
var pattern:RegExp = /.*\//;
var results:Array = pattern.exec(cardImageService.url);
results != null ? assetsPath = results[0] : assetsPath = defaultPath;
genericCardSide = assetsPath + cardImageFeed.genericCardSide[0].url;
initMemoryGame();
}
private function cardImageFaultHandler(event:FaultEvent):void {
Alert.show(event.fault.message, "Could not load cardImage feed");
}
private function initMenu():void {
myMenu = new Menu();
var dp:Object = [{label: "Endangered species"}, {label: "Power sources"}];
myMenu.dataProvider = dp;
myMenu.selectedIndex = 0;
myMenu.addEventListener("itemClick", itemClickHandler);
popB.popUp = myMenu;
popB.label = myMenu.dataProvider[myMenu.selectedIndex].label;
}
private function itemClickHandler(event:MenuEvent):void {
var label:String = event.item.label;
cardImageService.url = myXMLDataArray[event.index];
popB.label = label;
popB.close();
myMenu.selectedIndex = event.index;
cardImageService.send();
}
]]>
</mx:Script>
<mx:Text width="100%" color="blue" text="Flip two matching cards"/>
<mx:Tile direction="horizontal" borderStyle="inset"
horizontalGap="10" verticalGap="15"
paddingLeft="10" paddingTop="10" paddingBottom="10" paddingRight="10">
<mx:Repeater id="rp" dataProvider="{gameArray}">
<mx:Image id="card" width="160" height="160"
source="{setGenericCardSide()}"
click="reveal(event.currentTarget.repeaterIndices)"
horizontalAlign="center" verticalAlign="middle"/>
</mx:Repeater>
</mx:Tile>
<mx:HBox>
<mx:HBox borderStyle="solid" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
<mx:HBox borderStyle="solid">
<mx:Label id="scoreLabel" height="20" width="100" text=""/>
<mx:Label id="totalClicksLabel" height="20" width="100" text=""/>
</mx:HBox>
<mx:Button id="newGameButton" height="20" label="New Game" click="initMemoryGame()"/>
</mx:HBox>
<mx:VBox horizontalAlign="center">
<mx:Text color="black" text="Choose your theme"/>
<mx:PopUpButton id="popB" label="Edit" creationComplete="initMenu();" width="240" />
</mx:VBox>
</mx:HBox>
</mx:Panel>
</mx:Application>
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.