Create a Flash game like Gold Miner – AS3 version

Read all posts about "" game

After more than two years since the release of Gold Miner tutorial, Luke Mikelonis wants to share with us the AS3 version he made to to practice up a bit on AS3.

There are four classes: Main (the main one), Boulders, Pod, Rod.

There isn’t that much theory to explain as the basics have been already discussed in the original post.

Anyway, this is Main class:

package {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	
	public class Main extends MovieClip {
		
		private var boulderData:Array;
		private var boulders:Array = [];
		
		private var pod:Pod;
		
		private var left:Boolean = false;
		private var right:Boolean = false;
		
		public function Main() {
			boulderData = [[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]];
			
			placeBoulders();
			placePod();
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClick);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
			addEventListener(Event.ENTER_FRAME, updateStatus);
		}
		
		private function placeBoulders():void {
			for (var i in boulderData) {
				var boulder:Array = boulderData[i];
				var newBoulder:Boulder = new Boulder(boulder[0], boulder[1], boulder[2]);
				
				addChild(newBoulder);
				boulders.push(newBoulder);
			}
		}
		
		private function placePod():void {
			pod = new Pod();
			addChild(pod);
			pod.createRod();
		}
		
		public function getPod():Pod {
			return pod;
		}
		
		private function onMouseClick(e:MouseEvent):void {
			pod.shootPod();
		}
		
		private function updateStatus(e:Event):void {
			if (!left && !right) {
				pod.setSpeed(0);
			} else if (left && right) {
				pod.setSpeed(0);
			} else if (left) {
				pod.setSpeed(-4);
			} else {
				pod.setSpeed(4);
			}
			
			for (var i in boulders) {
				boulders[i].updateStatus();
				if (boulders[i].remove) {
					removeBoulder(boulders[i]);
					boulders.splice(i, 1);
				}
			}
		}
		
		private function removeBoulder(boulder:Boulder) {
			removeChild(boulder);
			boulder = null;
		}
		
		private function onKeyPress(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37:
					left = true;
					break;
				
				case 39:
					right = true;
					break;
			}
		}
		
		private function onKeyRelease(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37:
					left = false;
					break;
				
				case 39:
					right = false;
					break;
			}
		}
	}
}

This is Boulder class:

package {
	
	import flash.display.MovieClip;
	
	public class Boulder extends MovieClip {
		
		private var picked:Boolean = false;
		private var gotBoulder:Boolean = false;
		
		public function Boulder(xPos:Number, yPos:Number, boulderWidth:Number) {
			x = xPos;
			y = yPos;
			width = height = boulderWidth;
		}
		
		public function updateStatus():void {
			var main:Main = this.parent as Main
			var pod:Pod = main.getPod();
			if (!picked) {
				if (pod.status == "shoot" && this.hitTestPoint(pod.hotSpotX, pod.hotSpotY)) {
					pod.status = "rewind";
					picked = true;
					pod.slowdown = Math.floor(width / 5);
				}
			} else {
				x = pod.hotSpotX;
				y = pod.hotSpotY;
				
				if (pod.status == "rotate") {
					gotBoulder = true;
				}
			}
		}
		
		public function get remove():Boolean {
			return gotBoulder;
		}
	}
}

This is Pod class:

package {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Pod extends MovieClip {
		
		private var rotationDir:Number = 2;
		private var podStatus:String = "rotate";
		private var slowDown:Number = 0;
		private var speed:Number = 0;		
		private var maxSpeed:Number = 4;
		private var currentDir:Number = 0;
		private var startRopeX:Number;
		
		public var hotSpotX:Number;
		public var hotSpotY:Number;
		
		private var rod:Rod;
		
		public function Pod() {
			x = 250;
			y = 0;
			
			addEventListener(Event.ENTER_FRAME, updateStatus);
		}
		
		public function createRod():void {
			rod = new Rod();
			this.parent.addChild(rod);
		}
		
		private function move():void {
			x += speed;
		}
		
		public function setSpeed(newSpeed:Number):void {
			speed = newSpeed;
			if (speed > maxSpeed) {
				speed = maxSpeed;
			} else if (speed < -maxSpeed) {
				speed = -maxSpeed;
			}
		}
		
		public function shootPod():void {
			if (podStatus == "rotate") {
				currentDir = (rotation + 90) * 0.0174532925;
				startRopeX = x;
				podStatus = "shoot";
			}
		}
		
		private function updateStatus(e:Event):void {
			switch (podStatus) {
				case "rotate":
					rotation += rotationDir;
					if (rotation >= 80 || rotation <= -80) {
						rotationDir *= -1;
					}
					
					move();
					break;
				
				case "shoot":
					slowDown = 0;
					x += 10 * Math.cos(currentDir);
					y += 10 * Math.sin(currentDir);
					
					hotSpotX = x + 40 * Math.cos(currentDir);
					hotSpotY = y + 40 * Math.sin(currentDir);
					
					if (hotSpotY > 400 || hotSpotX < 0 || hotSpotX > 500) {
						podStatus = "rewind";
					}
					
					rod.clear();
					rod.lineStyle(1, 0x000000);
					rod.moveTo(startRopeX, 0);
					rod.lineTo(x, y);
					break;
				
				case "rewind":
					rod.clear();
				
					hotSpotX = x + 40 * Math.cos(currentDir);
					hotSpotY = y + 40 * Math.sin(currentDir);
					
					x -= (10 - slowDown) * Math.cos(currentDir);
					y -= (10 - slowDown) * Math.sin(currentDir);
					
					if (y < 0) {
						y = 0;
						x = startRopeX;
						podStatus = "rotate";
					} else {
						rod.lineStyle(1, 0x000000);
						rod.moveTo(startRopeX, 0);
						rod.lineTo(x, y);
					}
					break;
			}
		}
		
		public function get status():String {
			return podStatus;
		}
		
		public function set status(status:String):void {
			podStatus = status;
		}
		
		public function set slowdown(slowdown:Number):void {
			slowDown = slowdown;
		}
	}
}

And finally Rod class:

package {
	
	import flash.display.Sprite;
	import flash.display.Graphics;
	
	public class Rod extends Sprite {
		
		public function clear():void {
			graphics.clear();
		}
		
		public function lineStyle(thick:Number, color:uint):void {
			graphics.lineStyle(thick, color);
		}
		
		public function moveTo(xPos:Number, yPos:Number):void {
			graphics.moveTo(xPos, yPos);
		}
		
		public function lineTo(xPos:Number, yPos:Number):void {
			graphics.lineTo(xPos, yPos);
		}
	}
}

This is the result:

Use arrow keys to move the Pod, and mouse button to fire.

Download the source code. Hope this helps you to convert AS2 scripts to AS3.

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