Quantcast
Channel: Emanuele Feronato | RSS Feed
Viewing all articles
Browse latest Browse all 3

Create a Flash game like Gold Miner – AS3 version

$
0
0

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.

Want to learn more? Learn by example!

Get the full commented source code of an actual commercial cross platform HTML5 game!!


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images