Flash CS3 - A Simple Game



game.as

package {
import flash.display.*;
import flash.ui.Mouse;
import flash.events.*;
import flash.text.*;
public class game extends Sprite {
var hits:int=0;
var misses:int=0;
var shots:int=0;
var cDepth:int=100;
var level:int=1;
var xSpeed:Number=3;
var stageWidth:Number=550;
var stageHeight:Number=300;

var AirCraft_MC:MovieClip = new MovieClip();

var AimMC:MovieClip=new Aim_mc;//MovieClip to aim your gun - we import this MovieClip direct from Lib
var stats_txt:TextField=new TextField;// create a text field to display the player's statistics.
public function game():void {
black_mc.start_btn.addEventListener(MouseEvent.MOUSE_DOWN ,StartFunction);
black_mc.start_btn.buttonMode=true;
}
private function StartFunction(event:MouseEvent ):void {
black_mc.visible =false;
aim();
stats_txt_fn();
stage.addEventListener(MouseEvent.MOUSE_MOVE,HideMouse);
stage.addEventListener(MouseEvent.MOUSE_DOWN,GunShot);
stage.addEventListener(Event.ENTER_FRAME,addAirCrafts);
}
private function aim() {
AimMC.mouseEnabled=false;
addChild(AimMC);
}
private function stats_txt_fn() {
var MyFormat:TextFormat=new TextFormat;
MyFormat.bold=true;
MyFormat.font="Arial";
MyFormat.size=12;
MyFormat.color=0xFFFFFF;
stats_txt.x=10;
stats_txt.y=0;
stats_txt.width=550;
stats_txt.height=20;
addChild(stats_txt);
// apply the TextFormat to the text field.
stats_txt.defaultTextFormat=MyFormat;
stats_txt.selectable=false;
updateStats();
}
private function HideMouse(event:MouseEvent):void {
Mouse.hide();
AimMC.x=event.stageX;
AimMC.y=event.stageY;
}
private function GunShot(event:MouseEvent):void {
shots++;
updateStats();
}
private function updateStats() {
var targetsHit:Number=Math.round(( hits * 100)/(hits+misses));
var accuracy:Number=Math.round( (hits * 100)/shots);
if (isNaN(targetsHit)) {
targetsHit=0;
}
if (isNaN(accuracy)) {
accuracy=0;
}
stats_txt.text="shots:" + shots + "\t" + "hits: " + hits + "\t" + "misses: " + misses + "\t" + "targets hit: " + targetsHit + "%" + "\t" + "accuracy: " + accuracy + "%" + "\t" + "level:" + level;
}
//new aircrafts are constantly added to the game
private function addAirCrafts(event:Event):void {
// randomly add new target's to the Stage.
if (randNum(1,40) == 1) {
var Current_MC:MovieClip;
// attach a new instance of the AirCraft instance from the library onto the Stage, and give it a unique depth.
var randomAirCraftNum:Number=randNum(1,2);
switch (randomAirCraftNum) {
case 1 :
Current_MC=new AirCraft1_mc ;
break;
case 2 :
Current_MC=new AirCraft2_mc ;
break;
default :
return;
break;
}
cDepth++;
// set the starting postition of the current target movie clip so it is just off to the left of the Stage.
Current_MC.x=- Current_MC.width;
//This is used to set the current movie clip's scale & speed
var scale:int=randNum(1,3);
//set the _xscale and _yscale properties of the current movie clip.
Current_MC.scaleX=1 - scale / 10;
Current_MC.scaleY=1 - scale / 10;
Current_MC.speed=xSpeed + randNum(0,3) + level;
//instead of all targets flying along the same path,they vary their vertical position slightly.
Current_MC.y=Math.round(Math.random() * 50) + 50;
Current_MC.name="AirCraft" + cDepth;
// Update the target's position on the Stage.
Current_MC.addEventListener(Event.ENTER_FRAME,AirCraft_EnterFrameHandler);
Current_MC.addEventListener(Event.ENTER_FRAME, AirCraft_Remove);
Current_MC.addEventListener(MouseEvent.CLICK, AirCraft_ClickHandler);
addChild(Current_MC);
//swap the custom cursor to the higher depth
swapChildren(Current_MC,AimMC);
}
}
// create a function that returns a random integer
private function randNum(minNum:Number,maxNum:Number):Number {
return Math.floor(Math.random() * maxNum - minNum + 1) + minNum;
}
private function AirCraft_EnterFrameHandler(event:Event):void {
AirCraft_MC=event.currentTarget as MovieClip;
// move the target horizontally along the Stage.
AirCraft_MC.x+= AirCraft_MC.speed;
// slightly decrement the _y position of the current target movie clip.
AirCraft_MC.y-= 0.4;

}
//delete the instance.
function AirCraft_Remove(event:Event):void {
if (AirCraft_MC.x > stageWidth) {
misses++;
updateStats();
//trace(AirCraft_MC.name);
removeChild(AirCraft_MC);
AirCraft_MC.removeEventListener(Event.ENTER_FRAME,AirCraft_Remove);
AirCraft_MC.removeEventListener(Event.ENTER_FRAME, AirCraft_EnterFrameHandler);

} else if (AirCraft_MC.y > stageHeight) {
removeChild(AirCraft_MC);
AirCraft_MC.removeEventListener(Event.ENTER_FRAME,AirCraft_Remove);
AirCraft_MC.removeEventListener(Event.ENTER_FRAME, AirCraft_EnterFrameHandler);

}
}
// when we hit on the airCraft
function AirCraft_ClickHandler(event:MouseEvent):void {
var Clicked_AirCraft_MC:MovieClip = event.currentTarget as MovieClip;
// update the player's stats
hits++;
if ((hits%40) == 0) {
level++;
}
updateStats();
//go to the movie clip's label named "hit"
Clicked_AirCraft_MC.gotoAndPlay("hit");
// create an onEnterFrame event for the current movie clip instance.
Clicked_AirCraft_MC.addEventListener(Event.ENTER_FRAME, AirCraft_HitEnterFrameHandler);
//delete the onPress event handler.
Clicked_AirCraft_MC.removeEventListener(MouseEvent.CLICK, AirCraft_ClickHandler);

}
function AirCraft_HitEnterFrameHandler(event:Event):void {
var AirCraft_Damage:MovieClip = event.currentTarget as MovieClip;
// animate the target falling from the sky.
var gravity:int = 10;
var ymov:int = AirCraft_Damage.y + gravity;
// increment the rotation of the current movie clip clock-wise
AirCraft_Damage.rotation += 20;
// falling from the sky instead of just dropping straight down.
AirCraft_Damage.x += xSpeed;
AirCraft_Damage.y = ymov;

}

}
}

Comments

Popular posts from this blog

AS3 - Access objects from external SWF files

Flash CS3 - XML Guestbook (AS3)

Flash CS4 - 3D Rotating Menu