|
Projects -
Flash CS3
|
|
Written by drooza
|
|
Tuesday, 23 September 2008 19:02 |
|
Page 3 of 9 Without the comments, TileGame will be 572 lines of code. Since thats quite a few for an article such as this, we'll split the code up into several sections: import statements, class variables used and initialization methods; end of level/game functions; intializing level / game function; in-game functions; and last but not least clumping functions! Import Statements, Variables Used and Game Initialization Methods First we'll start with the first 100 lines of code which basically set the game up and "run" the game. /** TileGame.as Tile Game engine using an array of arrays full of instances of * type Tile. idea of the game: A group of colored tiles that when 2 or more * are grouped together may be destroyed in exchange for points. If you * destroy all the blocks on the field, or you gain enough points, you advance * to the next level. * * @see Tile.as * @author Drooza */
package { import flash.display.MovieClip; import flash.display.Stage; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import fl.transitions.easing.*; import fl.transitions.Tween; import fl.transitions.TweenEvent; public class TileGame extends MovieClip { private var colMax:int; //number of columns private var rowMax:int; //number of rows private var tileList:Array; //Will become Array of arrays private var types:Number; //number of different colors // in play private var score:Number; private var currentLevelScore:Number; private var level:int; private var levelScoreToBeat:Number; public var score_txt:TextField; public var gameState:int; private var CONTINUE:int; private var GAMEOVER:int; private var NEXTLEVEL:int; private var START:int; private var nextLevel_mc:NextLevelButton; //instantiated library items private var playAgain_mc:PlayAgainButton; // added to stage using as3 /** Class constructor. Sets the number of colors to start with sets * game states. */ public function TileGame() { types = 4; CONTINUE = 0; GAMEOVER = 1; NEXTLEVEL = 2; START = 3; stage.scaleMode = StageScaleMode.NO_SCALE; gameState = START; addEventListener(Event.ADDED_TO_STAGE, init); } /** Set the dimensions of the array and instantiates tileList. * Here game engine is started. */ private function init(e:Event):void { colMax = 13; rowMax = 11; tileList = new Array(colMax);
initScoreField(); addEventListener(Event.ENTER_FRAME, onEnterFrame); } /** The heart of the game that runs every frame. * */ private function onEnterFrame(e:Event):void { switch(gameState) { case CONTINUE: break; case GAMEOVER: trace("game over"); showEndPopUp(); break; case NEXTLEVEL: trace("next level!"); if(currentLevelScore >= levelScoreToBeat) { nextLevelPopUp(); level ++; } else { gameState = GAMEOVER; } break; case START: trace ("start"); resetGameVariables(); fillArray(types); showTiles(); //break; //no break needed to make gameState = CONTINUE default: gameState = CONTINUE; }
setScore(); } /** Instantiates variables for a new game. * */ private function resetGameVariables():void { trace("scores reset"); level = 1; types = 3; score = 0; currentLevelScore = 0; setScoreToBeat(); } /** Fills tileList with Tile instantiations and assigns click handlers * to each tile. */ private function fillArray(differentTypes:int):void { for(var col:int = 0; col < colMax; col ++) { //trace("Creating new row at column " + col); tileList[col] = new Array(rowMax); for(var row:int = 0; row < rowMax; row ++) { //trace("Creating tile."); var tile:Tile = new Tile(); tile.column = col; tile.row = row; tile.x = (tile.width * col) + 15; tile.y = -(tile.height * 2); addChild(tile); tileList[col][row] = tile; if(tile.currentFrame > differentTypes) { tile.gotoAndStop(Math.random() * differentTypes); } tile.addEventListener(MouseEvent.CLICK, tileClickHandler); } } } Methodology is self explanatory thus far. What you should see in this section is that it sets up the actual game engine in the onEnterFrame callback function and creates an array of arrays filled with instances of Tile. This is the data structure used to make this game. This algorithm is O(n2) because of the nested for-loops. For this type of game, it should not really matter because there will never be more than a couple hundred tiles in play at any given time. Optimizations would be futile because although the algorithm can change to something like O(n2 - n) it would still be O(n2). I stayed away from over optimization. The only "optimizatio" I made was to add that checked or not checked boolean variable to the tiles to remove any double checking for similar colors.
|
|
Last Updated ( Saturday, 22 November 2008 13:05 )
|