Create a Flash game like Rebuild Chile – Step 4: removing debris
Time to see the feature that will allow you to complete a level: removing debris.
Debris can be removed in two ways:
1) Pushing them off the stage through an hole in the border walls
2) Calling an helicopter and removing them on the fly
Obviously pro players will prefer the first option.
First, we need two new tile types: border wall and hole in border wall. The border wall is unwalkable, while the hole in the border wall is still unwalkable but debris can be pushed on it. Once on this tile, the debris disappear.
Also, pressing “H” you can call an helicopter, select a debris with arrow keys and removing pressin SPACEBAR or trying to make it without the helicopter pressing “H” again.
This is the complete, fully commented 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | package { import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.events.Event; public class rebuild extends Sprite { // array to store the map public var map:Array=new Array(); // array to store bulldozer position public var bulldozer_pos:Array= new Array(); // tree movieclip public var tree:tree_mc; // debris movieclip public var debris:debris_mc; // bulldozer movieclip public var bulldozer:bulldozer_mc; // external wall movieclip public var ext_wall:ext_wall_mc; // helicopter movieclip public var heli:heli_mc; // variable to store the last key pressed public var key_pressed:int=0; // flag to determine if the bulldozer is moving or not public var bulldozer_is_moving:Boolean=false; // bulldozer x and y direction public var bulldozer_x_dir:int=0; public var bulldozer_y_dir:int=0; // variable to determine which debris the helicopter is going to pick up public var heli_on_debris:int=1; // number of debris left to clear the screen public var debris_left:int=0; // variable to store the name of the debris we will move public var debris_to_move:String=""; // main function public function rebuild():void { // initializing the map // 0: empty space (walkable) // 1: tree (unmovable object) // 2: debris (movable object) // 9: external wall (unmovable object) // 10: hole in external wall (unwalkable, but debris can be pushed on it) map=[[9,9,9,9,9,9,9,10,9,9,9],[9,0,0,2,0,0,0,0,0,0,9],[9,0,0,0,0,0,0,0,0,0,9],[9,1,1,1,1,1,0,1,0,0,9],[9,1,0,0,2,0,0,1,0,0,9],[9,1,0,0,0,0,0,1,0,0,9],[9,1,1,1,1,1,1,1,0,0,9],[9,0,0,0,0,2,0,0,0,0,9],[9,0,0,0,0,0,0,0,0,0,9],[9,0,0,0,0,0,0,0,0,0,9],[9,9,9,9,9,9,9,9,9,9,9]]; // initializing bulldozer starting position bulldozer_pos=[7,3]; // calling main functions draw_map(); place_bulldozer(); // adding listeners stage.addEventListener(KeyboardEvent.KEY_DOWN, on_key_down); stage.addEventListener(KeyboardEvent.KEY_UP, on_key_up); addEventListener(Event.ENTER_FRAME,on_enter_frame); } // function to draw the map public function draw_map():void { // looping though all map. What's that "11"? it's the map width/height for (var i:int=0; i<11; i++) { for (var j:int=0; j<11; j++) { switch (map[j][i]) { // I found a tree case 1 : // placing the tree tree = new tree_mc(); // What's that "50"? it's the tile width/height // And the "25"? It's half a tile width/height // the whole game area is shifted by half a tile up and left // so that the external wall is only displayed at a half tree.x=i*50-25; tree.y=j*50-25; addChild(tree); break; case 2 : // placing a debris, same way as the tree debris=new debris_mc(); debris.name="debris_"+j+"_"+i; debris.x=i*50-25; debris.y=j*50-25; addChild(debris); debris_left++; break; case 9 : // placing an external wall, same way as the tree ext_wall = new ext_wall_mc(); ext_wall.x=i*50-25; ext_wall.y=j*50-25; addChild(ext_wall); break; } } } } // function to place the bulldozer, in the same way as the tree public function place_bulldozer():void { bulldozer = new bulldozer_mc(); bulldozer.x=50*(bulldozer_pos[1])-25; bulldozer.y=50*(bulldozer_pos[0])-25; addChild(bulldozer); } // function to be executed every time a key is down public function on_key_down(e:KeyboardEvent):void { // storing key_pressed variable the keyCode of the latest key pressed key_pressed=e.keyCode; } // function to be executed every time a key is released public function on_key_up(e:KeyboardEvent):void { // if we are releasing the same key we pressed last time, then reset key_pressed variable // this way we allow to press only one key at time if (e.keyCode==key_pressed) { key_pressed=0; } } // function to be executed at every frame public function on_enter_frame(e:Event):void { // I am checking for keyboard input only if the bulldozer is not already moving // and there is at least one debris to remove if (! bulldozer_is_moving&&debris_left>0) { switch (key_pressed) { // right case 37 : walk(-1,0); break; case 38 : // up walk(0,-1); break; case 39 : //left walk(1,0); break; case 40 : //down walk(0,1); break; case 72 : // "H", calling the helicopter // resetting key_pressed key_pressed=0; // if the helicopter in not on stage... if (heli==null) { // place the helicopter on the first debris and don't remove the debris heli=new heli_mc(); addChild(heli); place_heli(heli_on_debris,false); } else { // if the helicopter is already on stage, remove it and set the debris // to be picked up as the first one removeChild(heli); heli=null; heli_on_debris=1; } break; case 32 : // SPACEBAR, to remove selected debris // if the helicopter is on stage... if (heli!=null) { // remove the debris under the helicopter place_heli(heli_on_debris,true); // remove the helicopter itself removeChild(heli); heli=null; // set the debris to be picked up as the first one heli_on_debris=1; } break; } } // if the bulldozer is moving... if (bulldozer_is_moving) { // move the bulldozer one more step. One step = 10 pixels = 1/5 a tile bulldozer.x+=10*bulldozer_x_dir; bulldozer.y+=10*bulldozer_y_dir; // if there is a debris to move... if (debris_to_move!="") { // move the debris one more step with (getChildByName(debris_to_move)) { x+=10*bulldozer_x_dir; y+=10*bulldozer_y_dir; } } // if the bulldozer is completely on a tile... if ((bulldozer.x+bulldozer.y)%50==0) { // stop moving the bulldozer bulldozer_is_moving=false; // update bulldozer position bulldozer_pos[0]+=bulldozer_y_dir; bulldozer_pos[1]+=bulldozer_x_dir; // if there is a debris to move... if (debris_to_move!="") { // set the tile occupied by the debris BEFORE it moved to "0" (walkable) map[bulldozer_pos[0]][bulldozer_pos[1]]=0; // if the debris is on the map tile marked as "10" (hole in external wall)... if (map[bulldozer_pos[0]+bulldozer_y_dir][bulldozer_pos[1]+bulldozer_x_dir]==10) { // remove the debris removeChild(getChildByName(debris_to_move)); // decrease number of remaining debris debris_left--; } else { // if it's not on a "10" map, then simply update debris name and the map getChildByName(debris_to_move).name="debris_"+(bulldozer_pos[0]+1*bulldozer_y_dir)+"_"+(bulldozer_pos[1]+1*bulldozer_x_dir); map[bulldozer_pos[0]+bulldozer_y_dir][bulldozer_pos[1]+bulldozer_x_dir]=2; } // set the debris_to_move variable as "no debris to move" debris_to_move=""; } } } } // Function to move the bulldozer or the helicopter. px and py are the amount of tiles we want to move the bulldozer // Possible values passed: // 1,0: right // -1,0: left // 0,1: down // 0,-1: up public function walk(px:int,py:int):void { // if the helicopter is not on stage... if (heli==null) { // setting a "moved" flag as "false". It says the bulldozer did not move var moved:Boolean=false; // if the tile that the bulldozer should walk on is walkable... if (map[bulldozer_pos[0]+py][bulldozer_pos[1]+px]==0) { // the bulldozer moved moved=true; } else { // else, if the tile that the bulldozer should walk on is occupied by a debris and // the next one is a walkable tile or an hole in external wall... if (map[bulldozer_pos[0]+py][bulldozer_pos[1]+px]==2&&(map[bulldozer_pos[0]+2*py][bulldozer_pos[1]+2*px]==0||map[bulldozer_pos[0]+2*py][bulldozer_pos[1]+2*px]==10)) { // set which debris has to move debris_to_move=getChildByName("debris_"+(bulldozer_pos[0]+py)+"_"+(bulldozer_pos[1]+px)).name; // the bulldozer moved moved=true; } } // if the bulldozer moved... if (moved) { // setting variables to let it move and the direction it has to move bulldozer_is_moving=true; bulldozer_x_dir=px; bulldozer_y_dir=py; } } else { // if the helicopter is on stage... // reset key_pressed variable key_pressed=0; // increase heli_on_debris value by 1 if the direction is "down" or "right" // decrease it by 1 if the direction is "left" or "up" heli_on_debris+=px+py; // checking if we completely cycled through debris if (heli_on_debris<1) { heli_on_debris=debris_left; } if (heli_on_debris>debris_left) { heli_on_debris=1; } // place the helicopter on the selected debris and don't remove it place_heli(heli_on_debris,false); } } // function to place the helicopter on a debris and eventually remove it // n: n-th debris, starting from the upper left corner // del: if true, remove the debris public function place_heli(n:int,del:Boolean):void { // debris already found on the map var debris_found=0; // scanning the map... for (var i:int=1; i<10; i++) { for (var j:int=1; j<10; j++) { // if I found a debris... if (map[i][j]==2) { // increasing the number of debris I found debris_found++; // if I am on the right debris... if (debris_found==n) { // place the helicopter on the debris heli.x=j*50-25; heli.y=i*50-25; // if I have to remove the debris... if (del) { // update the map map[i][j]=0; // remove the debris removeChild(getChildByName("debris_"+i+"_"+j)); // update remaining debris debris_left--; } // exit the cycle j=10; i=10; } } } } } } } |
And this is the result:
Play just like you were playing the original game.
I enjoyed the making of this prototype so much that I am planning a game based on it, maybe with more options such as more ways to remove debris, and a different theme.
Any idea?
They can be easily customized to meet the unique requirements of your project.















(21 votes, average: 4.81 out of 5)









This post has 14 comments
neo
Super awesome :)
Bivis
Very nice!
Your “Create a Flash Game like…” posts r very useful!
Keep the good blog!
Matt Rouse
Hello.
I wrote a couple Facebook games in FBML and since they are in the process of deprecating it, I want to move into Flash games. I have flash experience, but I was wondering if you would be interested in colaborating on a game. I have the design roughed out already and could make all the graphics, database, php calls, etc. Any interest? Please mail me.
manoj
very good tutorial :)
but your coding style (everything in single .as file) is absolutely wrong, i know as long as you are making money with single .as file games its ok… but it will be much much better if you adopt OOPS concept… because many new aspirants game developer takes lesson from this( i m also one of those :) )
Emanuele Feronato
You’re absolutely right, but it’s easier to comment for a tutorial purpose.
And none of my AS3 games is on a single .as, as well as some examples you can find around the blog
Me
Thanks, love it when you teach us how to create new games, helps me a lot :)
GreenPuffle92
Thanks, when Rebuild Chile came out, I was wondering how to make it, :D
sam319
Thank u, it’s useful for me:)
Joseph Clover
Awesome!
Make a game with a snowy/icy background and you use a snowmobile to move iceblocks!
Then when you do, you can make your own ‘Mini-Town’!
:D
Wilson Silva
This level took me 6 key presses to finish. I’m a pro :D
ifun2
thanks very much dude
Rennan
Hi, my name is Rennan.
I´ve tried to use your files at download link. But, Helicoper function didn´t work. Have any problem at it?
Shawn Metcalf
You don’t publish your games in one file? I’ve been trying how to figure out how to get everything to work in one file (instead of multiple ones such as Kongregate’s Shootorial game/tutorial). Guess I should stop doing that.
Thanks for the site – it’s fantastic.
Ryan
hey , i like to ask what if we want to change the size of the debris. can we do that and how?