Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Rebuild Chile game, Actionscript 3, Flash and Game development.

In this second part we’ll see how to push debris.

It’s not that hard. Let me explain when your bulldozer can move in one direction:

The bulldozer can move in a direction if:

1) It does not go outside the stage

2) Next tile in the selected direction is empty

3) Next tile in the selected direction has a boulder and:

3.a) The tile next to the boulder in the selected direction is empty

3.b) Moving the boulder in the selected direction does not push it outside the stage (you can’t push debris out of the stage at the moment).

This can be optimized if we surround the play area with solid, invisible tiles. This way we won’t have to check for bulldozer and stuff to go outside the stage and will become even more handy when we will introduce the capability to push debris outside the stage.

Let me show you the script:

package {
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	public class rebuild extends Sprite {
		public var map:Array=new Array();
		public var bulldozer_pos:Array= new Array();
		public var tree:tree_mc;
		public var debris:debris_mc;
		public var bulldozer:bulldozer_mc;
		public var key_pressed:int=0;
		public function rebuild():void {
			map=[[9,9,9,9,9,9,9,9,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]];
			bulldozer_pos=[7,3];
			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=1; i<10; i++) {
				for (var j:int=1; j<10; j++) {
					switch (map[j][i]) {
						case 1 :
							tree = new tree_mc();
							tree.x=(i-1)*50;
							tree.y=(j-1)*50;
							addChild(tree);
							break;
						case 2 :
							debris=new debris_mc();
							debris.name="debris_"+j+"_"+i;
							debris.x=(i-1)*50;
							debris.y=(j-1)*50;
							addChild(debris);
							break;
					}
				}
			}
		}
		public function place_bulldozer():void {
			bulldozer = new bulldozer_mc();
			bulldozer.x=50*(bulldozer_pos[1]-1);
			bulldozer.y=50*(bulldozer_pos[0]-1);
			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 :
					walk(-1,0);
					break;
				case 38 :
					walk(0,-1);
					break;
				case 39 :
					walk(1,0);
					break;
				case 40 :
					walk(0,1);
					break;
			}
		}
		function walk(px,py):void {
			var moved:Boolean=false;
			if (map[bulldozer_pos[0]+py][bulldozer_pos[1]+px]==0) {
				moved=true;
			} else {
				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]=2;
					map[bulldozer_pos[0]+py][bulldozer_pos[1]+px]=0;
					with (getChildByName("debris_"+(bulldozer_pos[0]+py)+"_"+(bulldozer_pos[1]+px))) {
						x+=50*px;
						y+=50*py;
						name="debris_"+(bulldozer_pos[0]+2*py)+"_"+(bulldozer_pos[1]+2*px);
					}
					moved=true;
				}
			}
			if (moved) {
				bulldozer_pos[0]+=py;
				bulldozer_pos[1]+=px;
				bulldozer.x+=50*px;
				bulldozer.y+=50*py;
			}
		}
	}
}

And let's see what changed:

Line 13: Now the map is bigger because it contains the surrounding unwalkable tiles, marked with 9. As you can see, draw_map function does not ever consider them when it's time to draw the map. And we can skip all boundaries controls

Lines 56-71: Now the main function (it's not properly the main one, but it's the one I execute at every frame) does not move anything but calls walk function with two parameters: the horizontal and vertical offset. So walk(1,0) will mean I am going to walk to the right, walk(0,-1) means I am walking up, and so on.

And the walk function is the core of this script: it checks if the bulldozer can walk in the given direction (like I explained at the beginning of this post), moves (if possible) the bulldozer and moves (if possible) the boulder pushed by the bulldozer.

This is the tricky part. Look at line 33 how I assign a name to the debris movieclip. It's an unique name combining "debris" word and its x and y position in the map array.

So, if I want to select the debris at position (3,4) I know its name will be debris_3_4. That's how I select the debris movieclip to move at line 80. I know the position of the debris, so I know its name too, and I can easily select it with getChildByName function. Once I moved it, I change its name according to its new position, like I do at line 83.

And this is the result:

Arrow keys to move the bulldozer, try to push debris here and there.

No need to download, just replace the code in the first step with this one.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.