The Tile Engine: The Ins and Outs
squaring it all
Once again, the GameStage class controls the additioning of an element to the stage, and the removal of an element from stage. So there is a method to add/remove: player, player bullet, ai, ai bullet, the game over message and the explosion.
Of notice is the randomly picked X value for the AiTank when it first appears:
1 | ai.x = 75 + int(Math.random()*(200)) ; |
I made so that the AiTank can appear anywhere at the top of the maze.
The GameStage class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | package tiletank { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Rectangle; import flash.utils.setTimeout; import tiletank.tank.*; import tiletank.map.*; import tiletank.bullet.*; public class GameStage extends Sprite { private var _gameData:GameData; private var _stageElements:StageElements; private var _gameOver:GameOver; private var _gameContainer:Sprite; function GameStage () { _gameData = GameData.instance; _stageElements = StageElements.instance; _stageElements.addElement(this); x = 0; y = 45; } public function get gameBoundaries ():Rectangle { return _gameContainer.getBounds(this); } public function showInitStage ():void { if (_gameContainer && _gameContainer.stage) { clearStage(); ScreenManager.instance.clearBodies(); } _gameContainer = new Sprite(); addMaze(); var scores:GameInfo = _stageElements.getElementByClass(GameConstants.SCORES) as GameInfo; scores.totalEnemies = _gameData.totalEnemies - _gameData.enemiesDestroyed; _bg.gotoAndStop(0); addChild(_gameContainer); } public function showGameStage ():void { var scores:GameInfo = _stageElements.getElementByClass(GameConstants.SCORES) as GameInfo; scores.lives = ""; addPlayer(); var maze:Maze = _gameData.maze; _bg.gotoAndStop(maze.bg); } public function clearStage ():void { removeChild(_gameContainer); _gameContainer = null; } public function addAi ():void { if (_gameData.enemiesCreated >= _gameData.totalEnemies) return; _gameData.enemiesCreated++; var ai:AiTank = new AiTank(); ai.x = 75 + int(Math.random()*(200)) ; ai.y = 50; _gameContainer.addChild(ai); } public function removeAi (ai:BasicTank):void { addBoom(ai.x, ai.y, GameConstants.AI); _gameContainer.removeChild(ai); ai = null; _gameData.enemiesDestroyed++; if (_gameData.enemiesDestroyed == _gameData.totalEnemies) { dispatchEvent(new Event(GameConstants.GAME_PAUSED)); //*** LOGIC TO START A NEW LEVEL *** } _gameData.score += GameConstants.TANK_POINTS ; if (_gameData.enemiesCreated < _gameData.totalEnemies) { addAi(); } } public function addAiBullet (tank:BasicTank):AiBullet { var bullet:AiBullet = new AiBullet (tank); if (_gameData.maze.isColliding(bullet)) { return null; } else { _gameContainer.addChild(bullet); return bullet; } } public function addBoom (_x:Number, _y:Number, type:String):void { var b:Boom = new Boom(type); b.x = _x; b.y = _y; _gameContainer.addChild(b); setTimeout(function () { if (b && b.stage) _gameContainer.removeChild(b); b = null; }, GameConstants.TIME_BOOM); } public function removePlayer (tank:BasicTank):void { addBoom(tank.x, tank.y, GameConstants.PLAYER); if (tank.bullet) removeBullet(tank.bullet as BasicBullet); _bg.gotoAndPlay("hit"); _gameData.lives--; if (_gameData.lives != 0) { if (_gameData.enemiesDestroyed < _gameData.totalEnemies) { GameController.instance.startNewLife(); } } else { GameController.instance.gameOver(); } _gameContainer.removeChild(tank); } public function addPlayerBullet (tank:BasicTank):BasicBullet { var bullet:PlayerBullet = new PlayerBullet(tank); if (_gameData.maze.isColliding(bullet)) { return null; } else { _gameContainer.addChild(bullet); return bullet; } return null; } public function removeBullet(bullet:BasicBullet):void { if (!bullet) return; bullet.tank.bullet = null; if (bullet.stage) _gameContainer.removeChild(bullet); } public function showGameOver ():void { _gameOver = new GameOver(); _gameOver.x = 0; _gameOver.y = 180; _gameContainer.addChild(_gameOver); dispatchEvent(new Event(GameConstants.GAME_PAUSED)); stage.addEventListener (MouseEvent.MOUSE_UP, onGameOverClick); } private function addPlayer ():void { var tank:PlayerTank = new PlayerTank(); tank.x = 190; tank.y = 320; _gameContainer.addChild(tank); _gameData.gameMode = GameConstants.PLAY; } private function addMaze ():void { var maze:Maze = _gameData.addMaze(); _gameContainer.addChild(maze); _gameContainer.x = stage.stageWidth/2 - 200; } /** * if user clicks stage after game over, game is restarted */ private function onGameOverClick (event:MouseEvent):void { removeEventListener (MouseEvent.MOUSE_UP, onGameOverClick); GameController.instance.restart(); } } } |
This Post contains multiple pages:
Pages: 1 2 3 4 5 6 7 8 9 10 11

