How to Create a Platform Game in AS3 – Part 6

Part 6: Goals and Level Completion

Ok, now that we’ve added level components and enemies, we can finally make it possible to complete a level. In our game, we’re simply going to make an object that the player has to touch in order to get to the next level. We’re also going to add some coins that will rack up points along the way. The first thing we’re going to do for this is to add a score variable:

//SCORING VARIABLES
var mainScore:int = 0;

Next, we’re going to add coins. We’ll do this just like we added the level elements. This time, we’re going to make them into a class.

First, we’re going to have to create a new class called “Coin”. We’ll do this by creating a new external ActionScript file called “Coin.as” and adding the following code to it:

package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
	//sprites are just movieclips without any frames in them
	public class Coin extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;

		public function Coin(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}

		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//making an invisible box that will help in placement
			this.graphics.beginFill(0x000000,0);
			this.graphics.drawRect(0,0,25,25);
			//then adding a shape within it that will show the coin
			this.graphics.beginFill(0xFFFFFF,1);
			this.graphics.drawCircle(12.5,12.5,2.5);
		}
		private function eFrame(event:Event):void{
			//hit testing with the coin and the main guy
			//we'll use math for this!
			if(_root.mcMain.x <= this.x + _root.lvlHolder.x + 10
			   && _root.mcMain.x >= this.x + _root.lvlHolder.x - 10
			   && _root.mcMain.y <= this.y + 10
			   && _root.mcMain.y >= this.y - 10){
				this.parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, eFrame);
				//then we update the score!
				_root.mainScore += 100;
			}
		}
	}
}

The next thing to do is update the score whenever we kill an enemy. I hope you can do this on your own. I’m guessing that 500 points is a fair value for slaying an enemy. Now, we can add a goal object. I’m going to make mine turquoise, simply because it’s a cool color. To do this, we’re going to have to make another class. You know the drill:

package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
	//sprites are just movieclips without any frames in them
	public class Goal extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;

		public function Goal(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}

		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//making an invisible box that will help in placement
			this.graphics.beginFill(0x00FFFF);
			this.graphics.drawRect(0,0,25,25);
		}
		private function eFrame(event:Event):void{
			if(hitTestObject(_root.mcMain)){
				//advancing a level
				_root.lvlCurrent ++;
				_root.resetLvl();
			}
		}
	}
}

We also have to add this code to the makeLvl() function’s for loop, as always:

if (lvlArray[i] == 8){
	//the goal!
	newPlacement = new Goal();
	enemyHolder.addChild(newPlacement);
}

Finally, change the lvlArray1 and add a lvlArray2:

var lvlArray1:Array = new Array(
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,8,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,5,0,0,6,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);

Well, that’s it for goals and level completion. Next time, we’ll fix some bugs and add some finishing touches!

Preview

Download Source
(Requires Flash CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials