Section 1
//Bullet (Bullet)
package {
import flash.display.*;
public dynamic class Bullet extends MovieClip {
}
}//package
Section 2
//ButtonStart (ButtonStart)
package {
import flash.display.*;
public dynamic class ButtonStart extends MovieClip {
}
}//package
Section 3
//Enemy1 (Enemy1)
package {
import flash.display.*;
public class Enemy1 extends MovieClip {
public var yspeed:int;// = 5
public var xspeed:int;// = 5
public var mydirection:String;
public var rnd1:int;
public var dead:Boolean;
public function Enemy1(){
rnd1 = (Math.random() * 2);
if (rnd1 == 0){
mydirection = "left";
};
if (rnd1 == 1){
mydirection = "right";
};
}
public function action():void{
this.y = (this.y + yspeed);
if (mydirection == "right"){
this.x = (this.x + xspeed);
};
if (mydirection == "left"){
this.x = (this.x - xspeed);
};
if (this.x < 5){
mydirection = "right";
};
if (this.x > 755){
mydirection = "left";
};
if (this.y > 610){
dead = true;
};
}
}
}//package
Section 4
//Game (Game)
package {
import flash.events.*;
import flash.display.*;
import flash.utils.*;
public class Game extends MovieClip {
public var timer:Timer;
public var score:int;
public var rnd1:int;
public var i:int;
public var ii:int;
public var buttonstart:ButtonStart;
public var gametitle:Title;
public var hero:Hero;
public var scorebox:ScoreBox;
public var enemy1:Enemy1;
public var bullet:Bullet;
public var gameovermessage:GameOverMessage;
public var Left:Boolean;
public var Right:Boolean;
public var Space:Boolean;
public var BulletArray:Array;
public var EnemyArray:Array;
public function Game(){
timer = new Timer(28);
super();
timer.addEventListener(TimerEvent.TIMER, onTimer);
StartScreen();
}
public function StartScreen(){
var buttonstartfunction:Function;
buttonstartfunction = function (_arg1:MouseEvent){
removeChild(buttonstart);
removeChild(gametitle);
SetUp();
};
gametitle = new Title();
addChild(gametitle);
gametitle.x = 400;
gametitle.y = 100;
buttonstart = new ButtonStart();
addChild(buttonstart);
buttonstart.x = 407;
buttonstart.y = 327;
buttonstart.addEventListener(MouseEvent.CLICK, buttonstartfunction);
}
public function SetUp(){
hero = new Hero();
addChild(hero);
hero.x = 400;
hero.y = 500;
hero.dead = false;
scorebox = new ScoreBox();
addChild(scorebox);
scorebox.x = 25;
scorebox.y = 12;
score = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUpHandler);
BulletArray = new Array();
EnemyArray = new Array();
timer.start();
}
public function KeyDownHandler(_arg1:KeyboardEvent){
if (_arg1.keyCode == 39){
Right = true;
};
if (_arg1.keyCode == 37){
Left = true;
};
}
public function KeyUpHandler(_arg1:KeyboardEvent){
if (_arg1.keyCode == 39){
Right = false;
};
if (_arg1.keyCode == 37){
Left = false;
};
if (_arg1.keyCode == 32){
Space = true;
};
}
public function onTimer(_arg1:TimerEvent):void{
IntroduceEnemies();
ProcessUserInput();
MoveObjects();
CollisionDetection();
RemoveDeadObjects();
UpdateDisplay();
CheckForGameOver();
}
public function IntroduceEnemies(){
rnd1 = (Math.random() * 30);
if (rnd1 == 1){
enemy1 = new Enemy1();
addChild(enemy1);
enemy1.x = (Math.random() * 800);
enemy1.y = -30;
EnemyArray.push(enemy1);
};
}
public function ProcessUserInput(){
if (((Right) && ((hero.x < 780)))){
hero.x = (hero.x + 10);
};
if (((Left) && ((hero.x > 20)))){
hero.x = (hero.x - 10);
};
if (Space){
Shoot();
};
}
public function Shoot(){
Space = false;
bullet = new Bullet();
addChild(bullet);
bullet.x = hero.x;
bullet.y = hero.y;
bullet.dead = false;
BulletArray.push(bullet);
}
public function MoveObjects(){
var _local1:Object;
i = 0;
while (i < EnemyArray.length) {
EnemyArray[i].action();
i++;
};
for each (_local1 in BulletArray) {
_local1.y = (_local1.y - 15);
};
}
public function CollisionDetection(){
i = 0;
while (i < EnemyArray.length) {
if (EnemyArray[i].hitTestObject(hero.HitZone)){
hero.dead = true;
};
ii = 0;
while (ii < BulletArray.length) {
if (EnemyArray[i].hitTestObject(BulletArray[ii])){
EnemyArray[i].dead = true;
BulletArray[ii].dead = true;
score = (score + 5);
};
ii++;
};
i++;
};
}
public function RemoveDeadObjects(){
i = 0;
while (i < BulletArray.length) {
if (((BulletArray[i].dead) || ((BulletArray[i].y < 0)))){
removeChild(BulletArray[i]);
BulletArray[i] = null;
BulletArray.splice(i, 1);
};
i++;
};
i = 0;
while (i < EnemyArray.length) {
if (EnemyArray[i].dead){
removeChild(EnemyArray[i]);
EnemyArray[i] = null;
EnemyArray.splice(i, 1);
};
i++;
};
}
public function UpdateDisplay(){
scorebox.scoretext.text = ("Score: " + score.toString());
}
public function CheckForGameOver(){
if (hero.dead){
var gameoverfunction:Function = function (_arg1:MouseEvent){
RemoveAllObjects();
StartScreen();
};
timer.stop();
gameovermessage = new GameOverMessage();
addChild(gameovermessage);
gameovermessage.x = 370;
gameovermessage.y = 200;
gameovermessage.addEventListener(MouseEvent.CLICK, gameoverfunction);
};
}
public function RemoveAllObjects(){
i = 0;
while (i < BulletArray.length) {
removeChild(BulletArray[i]);
BulletArray[i] = null;
i++;
};
i = 0;
while (i < EnemyArray.length) {
removeChild(EnemyArray[i]);
EnemyArray[i] = null;
i++;
};
BulletArray = [];
EnemyArray = [];
removeChild(hero);
removeChild(gameovermessage);
removeChild(scorebox);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, KeyUpHandler);
}
}
}//package
Section 5
//GameOverMessage (GameOverMessage)
package {
import flash.display.*;
public dynamic class GameOverMessage extends MovieClip {
}
}//package
Section 6
//Hero (Hero)
package {
import flash.display.*;
public dynamic class Hero extends MovieClip {
public var HitZone:MovieClip;
}
}//package
Section 7
//ScoreBox (ScoreBox)
package {
import flash.display.*;
import flash.text.*;
public dynamic class ScoreBox extends MovieClip {
public var scoretext:TextField;
}
}//package
Section 8
//Title (Title)
package {
import flash.display.*;
public dynamic class Title extends MovieClip {
}
}//package