Tuesday, March 1, 2011

Final Flash Game

I was somewhat uncomfortable coding on my own which is why I went this route. Instead of copying and pasting the code in the tutorial however, I did write it myself while following so I could gain a better understanding of what I was doing. This made the process go a bit slower than expected so I didn't have much time to go back and try and add my own touches to the game. I'm still happy with the final product though and I did learn a bit along the way so even though it's so simple I hope it meets the requirements.

EDIT: If you don't like the sizing and such, you can see the full game Here.





CODE
Ship


package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.*;

public class Ship extends MovieClip
{
private var mouseIsDown:Boolean = false;
public var myManager:BulletManager = new BulletManager();
public var myHud:HudDisplay = new HudDisplay(stage);
public var myEnemyManager:EnemyManager = new EnemyManager(stage, myManager, myHud);
private var bulletInerval = setInterval(createBullet, 200);

public function Ship()
{
trace("Ship added to stage!");
stage.addEventListener(Event.ENTER_FRAME, moveShip);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
myEnemyManager.spawnEnemies(4, 1);
myEnemyManager.spawnEnemies(2, 2);
myEnemyManager.spawnEnemies(2, 3);
}
private function moveShip(Event):void
{
this.x+=(stage.mouseX-this.x)/5;
}
private function mDown(MouseEvent):void
{
mouseIsDown = true;
}
private function mUp(MouseEvent):void
{
mouseIsDown = false;
}
private function createBullet():void
{
if (mouseIsDown)
{
var aBullet:Bullet = new Bullet();
stage.addChild(aBullet);
aBullet.x = this.x;
aBullet.y = this.y;
myManager.bulletArray.push(aBullet);
}
}
}
}

Bullet
package
{
import flash.display.MovieClip;

public class Bullet extends MovieClip
{
public function Bullet()
{
trace("Bullet fired!");
}
}
}

Enemy Manager
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class EnemyManager extends MovieClip
{
public var enemyArray:Array = [];
private var _stage;
private var _bulletmanager;
private var _hud;
private var speeds:Array = [5,7,10];
private var explosionArray:Array = [];

public function EnemyManager(get_stage:Stage, manager, hud)
{
trace("Enemy manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
_stage = get_stage;
_bulletmanager = manager;
_hud = hud;
}
public function spawnEnemies(num:Number, enemytype:Number):void
{
for (var i:int=0; i{
var anEnemy:MovieClip = new Enemy();
anEnemy.gotoAndStop(enemytype);
var enemySpeed:Number = speeds[enemytype - 1];
enemyArray.push({mc: anEnemy, eSpeed:enemySpeed});
_stage.addChild(anEnemy);
anEnemy.x = Math.random() * 400;
anEnemy.y = - Math.random() * 400;
trace("Enemy spawned: " + anEnemy.height);
}
}
private function managing(Event):void
{
for (var u:int=0; u{
enemyArray[u].mc.y += enemyArray[u].eSpeed;
if (enemyArray[u].mc.y > 450)
{
if (enemyArray[u].mc.alpha == 1)
{
_hud.addMiss(1);
}
enemyArray[u].mc.y = - Math.random() * 400;
enemyArray[u].mc.x = Math.random() * 400;
enemyArray[u].mc.alpha = 1;
}

for (var g:int=0; g<_bulletmanager.bulletArray.length; g++)
{
if (_bulletmanager.bulletArray[g].alpha == 1 && enemyArray[u].mc.alpha == 1 && _bulletmanager.bulletArray[g].hitTestObject(enemyArray[u].mc))
{
enemyArray[u].mc.alpha = 0;
_bulletmanager.bulletArray[g].alpha = 0;
var anExplosion:MovieClip = new Explosion();
_stage.addChild(anExplosion);
anExplosion.x = enemyArray[u].mc.x;
anExplosion.y = enemyArray[u].mc.y;
explosionArray.push(anExplosion);
_hud.addScore(10);
}


}

}

for (var l:int=0; l{
if (explosionArray[l].currentFrame == 10)
{
_stage.removeChild(explosionArray[l]);
explosionArray.splice(l,1);
}

}

}
}
}

BulletManager
package
{
import flash.display.MovieClip;
import flash.events.Event;

public class BulletManager extends MovieClip
{
public var bulletArray:Array = [];

public function BulletManager()
{
trace("Manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
}

private function managing(Event):void
{
for (var i:int=0; i{
bulletArray[i].y -= 10;
if (bulletArray[i].y < -10)
{
bulletArray[i].parent.removeChild(bulletArray[i]);
bulletArray.splice(i,1);
}
}
}
}
}

HudDisplay
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.events.Event;

public class HudDisplay
{
public var playerScore:Number = 0;
public var playerMisses:Number = 0;
public var showScore:TextField = new TextField();
public var showMisses:TextField = new TextField();
private var _stage;

public function HudDisplay(get_stage:Stage)
{
_stage = get_stage;
_stage.addChild(showScore);
showScore.x = showScore.y = 10;
showScore.width = 50;
showScore.height = 24;
showScore.selectable = false;
showScore.text = "0";
showScore.background = true;
showScore.backgroundColor = 0xCCFDFD;
showScore.addEventListener(Event.ENTER_FRAME, depthChecker);
_stage.addChild(showMisses);
showMisses.x = 10;
showMisses.y = 40;
showMisses.width = 50;
showMisses.height = 24;
showMisses.selectable = false;
showMisses.text = "0";
showMisses.background = true;
showMisses.backgroundColor = 0xFF6464;
showMisses.addEventListener(Event.ENTER_FRAME, depthChecker);
}

public function addScore(amount:Number):void
{
playerScore += amount;
showScore.text = playerScore.toString();
}

public function addMiss(amount:Number):void
{
playerMisses += amount;
showMisses.text = playerMisses.toString();
}

private function depthChecker(Event):void
{
if (_stage.getChildIndex(showScore)!=(_stage.numChildren-1))
{
_stage.setChildIndex(showScore, _stage.numChildren-1);
}
if (_stage.getChildIndex(showMisses)!=(_stage.numChildren-2))
{
_stage.setChildIndex(showMisses, _stage.numChildren-2);
}
}
}
}

No comments:

Post a Comment