Talking about Rebuild Chile game, Actionscript 3, Flash and Game development.
You have three reasons to play Rebuild Chile:
1) It’s a good puzzle game
2) You will help the children from Chile affected by the earthquake/tsunami because all income will be donated to rebuild Chile
3) You are going to know how to create a game like it
The game is a Sokoban-like game, with fixed and movable items. So, you must be able to create a working prototype in a day. With your bulldozer, you can only push movable items, and only if they aren’t blocked by other movable items, and must push them out of the stage.
In this first part, we’ll cover map creation and basic player movement.
I am going to replicate level three. Here it is:
Since it’s a tile based game, it’s easy to build a level once you determine every tile’s content.
Each tile is represented by an unique coordinate.
In my case, 0
means the tile is empty, 1
means there is an unmovable item (a tree) and 2
means there is a movable item (a boulder).
And with a simple array you can build the entire map.
The bulldozer isn’t part of the map but it’s added later. As a general rule, the player and moving enemies shouldn’t be part of the map.
Here it is the script:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class rebuild extends Sprite {
// declaring the map
public var map:Array=new Array();
// 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;
// variable to store the last key pressed by the player
public var key_pressed:int=0;
public function rebuild():void {
// creating the map
// 0: empty space
// 1: tree
// 2: debris
map=[[0,0,2,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[1,1,1,1,1,0,1,0,0],[1,0,0,2,0,0,1,0,0],[1,0,0,0,0,0,1,0,0],[1,1,1,1,1,1,1,0,0],[0,0,0,0,2,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]];
// bulldozer position
bulldozer_pos=[6,2];
draw_map();
place_bulldozer();
stage.addEventListener(KeyboardEvent.KEY_DOWN, on_key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, on_key_up);
stage.addEventListener(Event.ENTER_FRAME,on_enter_frame);
}
public function draw_map():void {
for (var i:int=0; i<9; i++) {
for (var j:int=0; j<9; j++) {
switch (map[j][i]) {
case 1 :
// I found a tree
tree = new tree_mc();
tree.x=i*50;
tree.y=j*50;
addChild(tree);
break;
case 2 :
// I found a debris
debris=new debris_mc();
debris.x=i*50;
debris.y=j*50;
addChild(debris);
break;
}
}
}
}
public function place_bulldozer():void {
bulldozer = new bulldozer_mc();
bulldozer.x=50*bulldozer_pos[1];
bulldozer.y=50*bulldozer_pos[0];
addChild(bulldozer);
}
public function on_key_down(e:KeyboardEvent):void {
key_pressed=e.keyCode;
}
public function on_key_up(e:KeyboardEvent):void {
if (e.keyCode==key_pressed) {
key_pressed=0;
}
}
public function on_enter_frame(e:Event):void {
switch (key_pressed) {
case 37 :
if (bulldozer_pos[1]>0&&map[bulldozer_pos[0]][bulldozer_pos[1]-1]==0) {
bulldozer_pos[1]--;
bulldozer.x-=50;
}
break;
case 38 :
if (bulldozer_pos[0]>0&&map[bulldozer_pos[0]-1][bulldozer_pos[1]]==0) {
bulldozer_pos[0]--;
bulldozer.y-=50;
}
break;
case 39 :
if (bulldozer_pos[1]<8&&map[bulldozer_pos[0]][bulldozer_pos[1]+1]==0) {
bulldozer_pos[1]++;
bulldozer.x+=50;
}
break;
case 40 :
if (bulldozer_pos[0]<8&&map[bulldozer_pos[0]+1][bulldozer_pos[1]]==0) {
bulldozer_pos[0]++;
bulldozer.y+=50;
}
break;
}
}
}
}
Let's see the main functions:
draw_map: phyisically draws the level, placing movieclips in proper positions according to map
array. In this case, tile size is 50x50.
place_bulldozer: places the bulldozer in the level
on_key_down and on_key_up manage in the simplest way possible player's keyboard input. The first one just stores the last key pressed, and the second one resets last key pressed value if the player releases it.
on_enter_frame just checks if the player is pressing an arrow key, and moves the bulldozer in that way, if there is room to move (map
value is equal to zero) and if it won't leave the 9x9 grid.
And that's it...
Use arrow keys to move the bulldozer.
This is just the beginning... the bulldozer cannot move the debris and there is no place to discard them. But we'll see how to do it during next step.
** edit **: if you liked the game, consider visiting the official site
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.