Perfect maze generation – tile based version – AS3
December 8, 2008 by Emanuele Feronato
Filed Under Actionscript 3, Flex, Game design, Users contributions • 5 Comments
Filed Under Actionscript 3, Flex, Game design, Users contributions • 5 Comments
After publishing Perfect maze generation – tile based version written in Php, I got some emails asking for an AS3 version.
Pedro Taranto made the AS3 porting in less than a day, and here’s what you’ll get:
Click on the maze to generate a new one
And this is the source code:
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | package br.com.pedrotaranto { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; import flash.geom.Point; [SWF(backgroundColor = "#FFFFFF", width = '800', height = '600', frameRate = "30")] public class TiledMazeGen extends Sprite { //constants private const MAZE_WIDTH : uint = 20; private const MAZE_HEIGHT : uint = 20; private const TILE_SIZE : uint = 10; private const START_COLOR : uint = 0xFF0000; private const FINISH_COLOR : uint = 0x00FF00; private const WALL_COLOR : uint = 0x000000; private const WALKABLE_COLOR : uint = 0xDDDDDD; //directions private const NORTH : String = "N"; private const SOUTH : String = "S"; private const EAST : String = "E"; private const WEST : String = "W"; //variables private var _width : uint; private var _height : uint; private var _maze : Array; private var _moves : Array; private var _start : Point; private var _finish : Point; private var _container : Sprite; public function TiledMazeGen () : void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; stage.addEventListener(MouseEvent.CLICK, _generate); _width = MAZE_WIDTH * 2 + 1; _height = MAZE_HEIGHT * 2 + 1; _start = new Point(1, 1); //_finish = new Point(_height - 2, _width - 2); _container = new Sprite(); _generate(); } private function _generate ( event : MouseEvent = null ) : void { _initMaze(); _createMaze(); _drawMaze(); } private function _initMaze () : void { _maze = new Array(_width); for ( var x : int = 0; x < _height; x++ ) { _maze[x] = new Array(_height); for ( var y : int = 0; y < _width; y++ ) { _maze[x][y] = true; } } _maze[_start.x][_start.y] = false; } private function _createMaze () : void { var back : int; var move : int; var possibleDirections : String; var pos : Point = _start.clone(); _moves = new Array(); _moves.push(pos.y + (pos.x * _width)); while ( _moves.length ) { possibleDirections = ""; if ((pos.x + 2 < _height ) && (_maze[pos.x + 2][pos.y] == true) && (pos.x + 2 != false) && (pos.x + 2 != _height - 1) ) { possibleDirections += SOUTH; } if ((pos.x - 2 >= 0 ) && (_maze[pos.x - 2][pos.y] == true) && (pos.x - 2 != false) && (pos.x - 2 != _height - 1) ) { possibleDirections += NORTH; } if ((pos.y - 2 >= 0 ) && (_maze[pos.x][pos.y - 2] == true) && (pos.y - 2 != false) && (pos.y - 2 != _width - 1) ) { possibleDirections += WEST; } if ((pos.y + 2 < _width ) && (_maze[pos.x][pos.y + 2] == true) && (pos.y + 2 != false) && (pos.y + 2 != _width - 1) ) { possibleDirections += EAST; } if ( possibleDirections.length > 0 ) { move = _randInt(0, (possibleDirections.length - 1)); switch ( possibleDirections.charAt(move) ) { case NORTH: _maze[pos.x - 2][pos.y] = false; _maze[pos.x - 1][pos.y] = false; pos.x -=2; break; case SOUTH: _maze[pos.x + 2][pos.y] = false; _maze[pos.x + 1][pos.y] = false; pos.x +=2; break; case WEST: _maze[pos.x][pos.y - 2] = false; _maze[pos.x][pos.y - 1] = false; pos.y -=2; break; case EAST: _maze[pos.x][pos.y + 2] = false; _maze[pos.x][pos.y + 1] = false; pos.y +=2; break; } _moves.push(pos.y + (pos.x * _width)); } else { back = _moves.pop(); pos.x = int(back / _width); pos.y = back % _width; } } } private function _drawMaze () : void { var tile : Sprite; if ( contains(_container) ) { removeChild(_container) } _container = new Sprite(); addChild(_container); for ( var x : int = 0; x < _height; x++ ) { for ( var y : int = 0; y < _width; y++ ) { tile = (_maze[x][y] == true) ? _drawTile(WALL_COLOR) : _drawTile(WALKABLE_COLOR); tile.x = x * TILE_SIZE; tile.y = y * TILE_SIZE; _container.addChild(tile); } } //start tile tile = _drawTile(START_COLOR); tile.x = _start.x * TILE_SIZE; tile.y = _start.y * TILE_SIZE; _container.addChild(tile); //finish tile /*tile = _drawTile(FINISH_COLOR); tile.x = _finish.x * TILE_SIZE; tile.y = _finish.y * TILE_SIZE; _container.addChild(tile);*/ } private function _drawTile ( color : uint ) : Sprite { var tile : Sprite = new Sprite(); tile.graphics.beginFill(color); tile.graphics.drawRect(0, 0, TILE_SIZE, TILE_SIZE); tile.graphics.endFill(); return tile; } private function _randInt ( min : int, max : int ) : int { return int((Math.random() * (max - min + 1)) + min); } } } |
Download the source and enjoy!
5 Responses to “Perfect maze generation – tile based version – AS3”
Leave a Reply
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)





This is great and endless all the same time.
Great stuff, havent been on in over a year glad I came back
Alright!!! AS3 version. Thank you very much.
how to use?
Hi!
Thanks for the Maze generator! It`s very nice!
I used it with a bit of papervision.. http://www.webdesignmedia.ro/Maze.swf :P