Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
New folders
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 11, 2010
1 parent 9f538dc commit d9deac1
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 0 deletions.
83 changes: 83 additions & 0 deletions as3/Bullet.as
@@ -0,0 +1,83 @@
package NS
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;

public class Bullet extends Sprite
{

/**
* The speed of the plane
*/
private var _speed:Number = 8;

/**
* Bullets
*/
public static var arBullets:Array = new Array();

/**
* Constructor
*/
public function Bullet(x:int, y:int)
{
super();
var img:Bitmap = Main.main.library.getObject("bullet");
this.addChild(img);

this.x = x;
this.y = y;

this.width = 5;
this.height = 5;

this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);

NS.Bullet.arBullets.push(this);
}

/**
* Called on enter frame
*/
private function onEnterFrame(e:Event):void
{
this.y -= this._speed;

this.gb();
}

/**
* Garbage collector
*/
private function gb():void
{
if(this.y < this.height)
{
this.dispose();
}
}

/**
* Remove completely the object
*/
private function dispose():void
{
//Remove events
this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);

//Remove object
this.stage.removeChild(this);

for(var i:String in Bullet.arBullets)
{
if(Bullet.arBullets[i] == this)
{
Bullet.arBullets.splice(i, 1);
}
}
}

}
}
113 changes: 113 additions & 0 deletions as3/Enemy.as
@@ -0,0 +1,113 @@
package NS
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class Enemy extends Sprite
{
/**
* The speed of the plane
*/
private var _speed:Number = 2;

/**
* Constructor
*/
public function Enemy()
{
super();

var img:Bitmap = Main.main.library.getObject("meteorite");
this.addChild(img);

this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
}

/**
* Called after added on the stage
*/
private function onAddedToStage(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
this.x = (this.stage.stageWidth - this.width) * Math.random();
}

/**
* Called on enter frame
*/
private function onEnterFrame(e:Event):void
{
if(!Main.main.gameEnd)
{
this.move();
this.checkColPlane();
this.checkBullet();
this.gb();
}
}

/**
* Check the colision with the plane
*/
private function checkColPlane():void
{
var testPoint:Point = new Point(this.x + this.width / 2, this.y + this.height / 2);
if(Main.main.plane.stage != null && Main.main.plane.hitTestPoint(testPoint.x, testPoint.y))
{
Main.main.finish();
}
}

/**
* Check the colision with the bullets
*/
private function checkBullet():void
{
for(var i:String in Bullet.arBullets)
{
if((Bullet.arBullets[i] as Bullet).hitTestObject(this))
{
this.dispose();
break;
}
}
}

/**
* Move
*/
private function move():void
{
this.y += this._speed;
}

/**
* The garbage collector
*/
private function gb():void
{
if(this.stage != null)
{
if(this.y > this.stage.stageHeight)
{
this.dispose();
}
}
}

/**
* Remove completely the objet
*/
private function dispose():void
{
//Remove events
this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);

//Remove object
this.stage.removeChild(this);
}
}
}
119 changes: 119 additions & 0 deletions as3/Main.as
@@ -0,0 +1,119 @@
package NS
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.clearInterval;
import flash.utils.setInterval;

[SWF(width='500',height='500',backgroundColor='#ffffff',frameRate='50')]

public class Main extends Sprite
{
/**
* The plane
*/
public var plane:Plane;

/**
* The library
*/
public var library:Library;

/**
* Interval enemy
*/
private var enemyInterval:int;

/**
* End of game
*/
public var gameEnd:Boolean = false;

/**
* End of loading
*/
private var loadingEnd:Boolean = false;

/**
* Start of game
*/
private var gameStart:Boolean;

/**
* The main object
*/
public static var main:Main;

public function Main()
{
this.library = new Library();
this.library.addImage("plane", "images/plane.png");
this.library.addImage("bullet", "images/orange-bullet.gif");
this.library.addImage("meteorite", "images/meteorite_medium2.png");

this.library.addEventListener(Event.COMPLETE, this.finishLoading);

this.stage.addEventListener(MouseEvent.CLICK, this.start);

Main.main = this;

Key.initialize(this.stage);

this.library.load();
}

/**
* Finish the loading
*/
private function finishLoading(e:Event):void
{
this.loadingEnd = true;

if(this.gameStart)
{
this.start();
}
}

/**
* Start
*/
public function start(e:Event = null):void
{
this.stage.removeEventListener(MouseEvent.CLICK, this.start);

this.gameStart = true;

if(this.loadingEnd)
{
this.plane = new Plane();
this.stage.addChild(this.plane);

this.enemyInterval = setInterval(this.addEnemy, 500);
}
}

/**
* Finish
*/
public function finish():void
{
if(!this.gameEnd)
{
this.gameEnd = true;
this.plane.dispose();
clearInterval(this.enemyInterval);
}
}

/**
* Add enemy on the scene
*/
public function addEnemy():void
{
var enemy:Enemy = new Enemy();
this.stage.addChild(enemy);
}
}
}

0 comments on commit d9deac1

Please sign in to comment.