Create a Flash game like Gold Miner – step 2

Read all posts about "" game

I received a lot of emails asking me to add left-right movement to my Gold Miner clone and requiring more information about manually placing the boulders.

In order to add left-right movement, you need to check for left and right arrow keys and move the hook in the proper direction.

Just remember you don’t have to let the hook move out of the stage and to update the starting/ending points of the rope.

When you want to manually place boulders, you need an array.

In the one I created, every boulder is an array made this way

[x position, y position, diameter]

Then I created an array containing n boulder arrays where n is the number of boulder I wanted to be in the stage.

Here it is the commented code:

// boulders are stored in an array
// [0] is x pos
// [1] is y pos
// [2] is the width
// don't make boulders too large (<50) or the rewind won't work
// until you change slowdown formula
boulders = [[100, 100, 30], [200, 100, 40], [300, 100, 40], [400, 100, 30], [100, 200, 30], [200, 200, 40], [300, 200, 40], [400, 200, 30], [100, 300, 30], [200, 300, 40], [300, 300, 40], [400, 300, 30]];
for (x=0; x the boulder has not been picked by the hook
	// picked = true => the boulder has been picked
	bould.picked = false;
	// function to be executed at every frame for the boulder
	bould.onEnterFrame = function() {
		// if it's not been picked...
		if (!this.picked) {
			// check if the hook is in shoot mode and touched the boulder
			// you'll see later what do hot_spot_x and hot_spot_y mean
			if (pod_status == "shoot" and this.hitTest(hot_spot_x, hot_spot_y, true)) {
				// set the hook on rewind mode
				pod_status = "rewind";
				// mark this boulder as picked
				this.picked = true;
				// determining the slowdown according to boulder size
				slowdown = Math.floor(this._width/5);
			}
		} else {
			// the boulder has been picked, so move it as the hook moves
			this._x = hot_spot_x;
			this._y = hot_spot_y;
			// if the hook status changed to rotate
			// (this means: if the hook took a boulder and pulled it out to surface...
			if (pod_status == "rotate") {
				// remove the boulder
				this.removeMovieClip();
			}
		}
	};
}
// placing the hook on stage
_root.attachMovie("pod", "pod", _root.getNextHighestDepth(), {_x:250});
// creating an empty movie clip to draw the rope
_root.createEmptyMovieClip("rod", _root.getNextHighestDepth());
// this is the rotation direction and speed
rotation_dir = 2;
// hook initial status
pod_status = "rotate";
// slowdown malus
slowdown = 0;
// hook movement speed
pod_speed = 4;
// function the hook will execute at every frame
pod.onEnterFrame = function() {
	// getting pod status
	switch (pod_status) {
	case "rotate" :
		// moving the hook to the left if player presses left
		if (Key.isDown(Key.LEFT)) {
			this._x -= pod_speed;
			if (this._x<20) {
				this._x = 20;
			}
		}
		// moving the hook to the right if player presses left   
		if (Key.isDown(Key.RIGHT)) {
			this._x += pod_speed;
			if (this._x>480) {
				this._x = 480;
			}
		}
		// if the status is rotate, just rotate the hook according to rotation_dir      
		this._rotation += rotation_dir;
		if (this._rotation == 80 or this._rotation == -80) {
			// invert rotation_dir if the hook reaches its minimum (or maximum) rotation allowed
			rotation_dir *= -1;
		}
		break;
	case "shoot" :
		// the hook has ben shoot
		// (re)set slowdown malus to zero
		slowdown = 0;
		// moving the hook using trigonometry
		this._x += 10*Math.cos(dir);
		this._y += 10*Math.sin(dir);
		// determining the hot spot of the hook
		// the hot spot is the lowest corner of the hook (that acts like an harpoon in this case)
		hot_spot_x = this._x+40*Math.cos(dir);
		hot_spot_y = this._y+40*Math.sin(dir);
		// if the hot spot goes off the stage
		if (hot_spot_y>400 or hot_spot_x<0 or hot_spot_x>500) {
			// then rewind the hook
			pod_status = "rewind";
		}
		// draw a line from the hook starting position to its actual position       
		// this will simulate the rope
		rod.clear();
		rod.lineStyle(1, 0x000000);
		// once it started from (250,0), now that hook can move I am starting from (start_rope_x, 0)
		rod.moveTo(start_rope_x, 0);
		rod.lineTo(this._x, this._y);
		break;
	case "rewind" :
		// clearing the rope
		rod.clear();
		// rewinding the hook...
		// it may seem a nonsense determining the hot spot now, but I need id
		// to move the boulder (if I have any boulder attached to the hook)
		hot_spot_x = this._x+40*Math.cos(dir);
		hot_spot_y = this._y+40*Math.sin(dir);
		// moving the hook with slowdown malus (if any)
		this._x -= (10-slowdown)*Math.cos(dir);
		this._y -= (10-slowdown)*Math.sin(dir);
		// if the hook returns in its initial position...
		if (this._y<0) {
			// then reset its position and set its status to rotate
			this._y = 0;
			this._x = start_rope_x;
			pod_status = "rotate";
		} else {
			// drawing a line as seen in shoot status, but only if the hook is still in the air   
			rod.lineStyle(1, 0x000000);
			rod.moveTo(start_rope_x, 0);
			rod.lineTo(this._x, this._y);
		}
		break;
	}
};
// when the mouse is clicked...
_root.onMouseDown = function() {
	// if the status is rotate...
	if (pod_status == "rotate") {
		// save hook heading and convert it to radians
		dir = (pod._rotation+90)*0.0174532925;
		// setting rope start position... the last hook position
		start_rope_x = pod._x;
		// set pod status to shoot
		pod_status = "shoot";
	}
};

And here it is the result...

Left/right arrows to move, mousebutton to fire.

Download the source code and enjoy

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

214 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
// 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