Players will always try to cheat. When making a game, don't expect people to respect any policy of playing.
We must prevent the player from continuing to pick tiles when the script is waiting to let him see the second tile he picked.
We need another instance variable, of a new type. Change class level variables and constants by coding this way:
// class level variables and constants
private var pickedTiles:Array = new Array();
private const NUMBER_OF_TILES:uint=20;
private var pauseGame:Timer;
private var canPick:Boolean=true;
// end of class level variables and constants
Boolean variables can only have a true
or false
value. canPick
variable will decide whether the player can pick another tile or not. Initially, it's true
because the player can pick a tile when the game begins.
Now change the onTileClicked
function this way:
private function onTileClicked(e:MouseEvent) { if(canPick){ var picked:tile_movieclip=e.currentTarget as tile_movieclip; trace("you picked a "+e.currentTarget.cardType); // checking if the current tile has already been picked if (pickedTiles.indexOf(picked)==-1) { pickedTiles.push(picked); picked.gotoAndStop(picked.cardType+1); } // end checking if the current tile has already been picked // checking if we picked 2 tiles if (pickedTiles.length==2) { canPick=false; pauseGame=new Timer(1000,1); pauseGame.start(); if (pickedTiles[0].cardType==pickedTiles[1].cardType) { // tiles match!! trace("tiles match!!!!"); pauseGame.addEventListener(TimerEvent.TIMER_COMPLETE,removeTiles); } else { // tiles do not match trace("tiles do not match"); pauseGame.addEventListener(TimerEvent.TIMER_COMPLETE,resetTiles); } // no more pickedTiles = new Array(); } // end checking if we picked 2 tiles } }
The entire function is executed only if the player can pick a tile. And that's right. When the player picked the second tile, simply set canPick
value to false
and you're done. The player cannot pick anymore.
The last thing to complete the game is letting the player be able to pick tiles again once the game has covered/removed the tiles.
Change removeTiles
function this way:
private function removeTiles(e:TimerEvent) {
pauseGame.removeEventListener(TimerEvent.TIMER_COMPLETE,removeTiles);
pickedTiles[0].removeEventListener(MouseEvent.CLICK,onTileClicked);
pickedTiles[1].removeEventListener(MouseEvent.CLICK,onTileClicked);
removeChild(pickedTiles[0]);
removeChild(pickedTiles[1]);
pickedTiles = new Array();
canPick = true;
}
And do the same with resetTiles
function:
private function resetTiles(e:TimerEvent) {
pauseGame.removeEventListener(TimerEvent.TIMER_COMPLETE,resetTiles);
pickedTiles[0].gotoAndStop(NUMBER_OF_TILES/2+1);
pickedTiles[1].gotoAndStop(NUMBER_OF_TILES/2+1);
pickedTiles = new Array();
canPick = true;
}
Simply set canPick
value to false
and again enable the player to pick tiles.
Test the movie. No more cheating!
Now we could just sit and play, but we want more.