Create a Flash game like Rebuild Chile – Step 2: pushing debris

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers