Book Image

Flash Game Development by Example

By : Emanuele Feronato
Book Image

Flash Game Development by Example

By: Emanuele Feronato

Overview of this book

<p>You can't call yourself a Flash game developer unless you know how to build certain essential games, and can quickly use the skills and techniques that make them up.<br /><br />Flash Game Development by Example is an ultra-fast paced game development course. Learn step-by-step how to build 10 classic games. Each game introduces new game development skills, techniques, and concepts. By the end of the book you will have built ten complete games &ndash; and have the skills you need to design and build your own game ideas.<br /><br />The book starts with simple well known puzzle games: Concentration and Minesweeper. After learning the basics of game design you&rsquo;ll introduce AI with a four-in-a-row game. Then as you build your own versions of old arcade games such as Snake, Tetris, and Astro Panic. The book ends with a collection of modern casual classics.</p>
Table of Contents (17 chapters)
Flash Game Development by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Where to Go Now
Index

Preventing the player from cheating


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.