rendered paste body/*
instances on stage:
ship -- is the ship.
asteroids_mc - an empty movieclip that will contain all of the asteroids
bullets_mc - an empty movieclip that will contain all of the asteroids
Symbols set up for linking and attaching through ActionScript:
AsteroidLarge - The first and largest asteroid to be shown.
AsteroidMedium - The second asteroid...two of these will spawn from the large asteroid
bullet - The projectiles fired from the ship.
*/
var score:Number = 0;
var lives:Number = 3;
// INITIALIZATION =================================================
function startLevel():Void {
//trace("startLevel");
for (var i:Number = 0; i < 4; i++) {
createAsteroid("AsteroidLarge");
}
createShip();
}
startLevel();
// ASTEROIDS =======================================================
function createAsteroid(size:String, x:Number, y:Number):Void {
//trace("createAsteroid");
var depth:Number = asteroids_mc.getNextHighestDepth();
var ast:MovieClip = asteroids_mc.attachMovie(size, "asteroid" + depth, depth);
/*if (x == undefined) {
ast._x = Math.random() * Stage.width;
} else {
ast._x = x;
}*/
ast._x = x || Math.random() * Stage.width;
ast._y = y || Math.random() * Stage.height;
ast._rotation = Math.random() * 360;
ast.rv = Math.random() * 6 - 3;
ast.xv = Math.random() * 6 - 3;
ast.yv = Math.random() * 6 - 3;
ast.onEnterFrame = moveAsteroid;
ast.size = size;
}
function moveAsteroid():Void {
this._x += this.xv;
this._y += this.yv;
this._rotation += this.rv;
var radius:Number = this._width / 2;
if (this._x > Stage.width + radius) {
this._x = -radius;
} else if (this._x < -radius) {
this._x = Stage.width + radius;
}
if (this._y > Stage.height + radius) {
this._y = -radius;
} else if (this._y < -radius) {
this._y = Stage.height + radius;
}
}
//CREATE SHIP ====================================================
function createShip(xb:Number, yb:Number):Void {
trace("createShip");
var depth2:Number = ship_mc.getNextHighestDepth();
var ship:MovieClip = ship_mc.attachMovie("ship", "ship" + depth2, depth2);
ship._x = Math.random() * Stage.width;
ship._y = Math.random() * Stage.height;
ship.onEnterFrame = moveShip;
}
// PROJECTILE COLLISIONS ===============================================
function checkCollision(bullet:MovieClip):Void {
if (asteroids_mc.hitTest(bullet._x, bullet._y, true)) {
for (var clip:String in asteroids_mc) {
var ast:MovieClip = asteroids_mc[clip];
if (ast.hitTest(bullet._x, bullet._y, true)) {
spawnAsteroid(ast);
bullet.removeMovieClip();
break;
}
}
// Check win.
var counter:Number = 0;
for (var clip:String in asteroids_mc) {
counter++;
}
if (counter == 0) {
startLevel();;
}
}
}
function checkCollision2(ship:MovieClip):Void {
if (asteroids_mc.hitTest(ship._x, ship._y, true)) {
for (var clip:String in asteroids_mc) {
var ast:MovieClip = asteroids_mc[clip];
if (ast.hitTest(ship._x, ship._y, true)) {
shipDeath();
break;
}
}
}
}
function spawnAsteroid(ast:MovieClip):Void {
switch (ast.size) {
case "AsteroidLarge":
var nextSize:String = "AsteroidMedium";
score += 5;
//trace("You have a large asteroid!");
break;
case "AsteroidMedium":
score += 10;
// If it's medium...
break;
default:
// otherwise...
}
score_txt.text = score;
createAsteroid(nextSize, ast._x, ast._y);
createAsteroid(nextSize, ast._x, ast._y);
ast.removeMovieClip();
}
function shipDeath():Void {
trace ("hit test");
ship.removeMovieClip();
lives--;
break;
}
// SHIP CONTROLS
ship.xv = 0;
ship.yv = 0;
ship.accel = 0;
function degToRad(num:Number):Number {
return num * Math.PI / 180;
}
function moveShip():Void {
trace ("start moving");
var rads:Number = degToRad(ship._rotation);
var xa:Number = Math.cos(rads) * ship.accel;
var ya:Number = Math.sin(rads) * ship.accel;
ship.xv += xa;
ship.yv += ya;
var speed:Number = Math.sqrt((ship.xv * ship.xv) + (ship.yv * ship.yv));
//trace(speed);
if (speed > 10) {
ship.xv = Math.cos(rads) * 10;
ship.yv = Math.sin(rads) * 10;
}
ship._x += ship.xv;
ship._y += ship.yv;
// Check bounds
if (ship._x > Stage.width ) {
ship._x = 0;
} else if (ship._x < 0) {
ship._x = Stage.width;
}
if (ship._y > Stage.height) {
ship._y = 0;
} else if (ship._y < 0) {
ship._y = Stage.height;
}
}
function onEnterFrame():Void {
if (Key.isDown(Key.UP)) {
ship.accel = 0.5;
} else {
ship.accel = 0;
}
if (Key.isDown(Key.LEFT)) {
ship._rotation -= 10;
}
if (Key.isDown(Key.RIGHT)) {
ship._rotation += 10;
}
moveShip();
checkCollision2(ship);
}
Key.addListener(this);
function onKeyUp():Void {
if (Key.getCode() == Key.SPACE) {
var d:Number = bullets_mc.getNextHighestDepth();
var b:MovieClip = bullets_mc.attachMovie("bullet", "bullet" + d, d);
b._x = ship._x;
b._y = ship._y;
b._rotation = ship._rotation
var rads:Number = degToRad(ship._rotation);
b.xv = Math.cos(rads) * 20;
b.yv = Math.sin(rads) * 20;
b.life = 0;
b.onEnterFrame = function():Void {
this._x += this.xv
this._y += this.yv
this.life ++;
if (this.life > 8) {
this._alpha -=10; //alpha shift
}
if (this.life > 15) {
this.removeMovieClip();
}
checkCollision(this);
}
}
}