rendered paste body/*
instances on stage:
ship_mc -- 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");
}
}
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;
}
}
// 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_mc.removeMovieClip();
lives--;
break;
}
// EVERY BELOW THIS LINE IS THE SAME CODE AS WE WENT OVER LAST WEEK, FROM KEYBOARD CONTROL ============================
ship_mc.xv = 0;
ship_mc.yv = 0;
ship_mc.accel = 0;
function degToRad(num:Number):Number {
return num * Math.PI / 180;
}
function moveShip():Void {
var rads:Number = degToRad(ship_mc._rotation);
var xa:Number = Math.cos(rads) * ship_mc.accel;
var ya:Number = Math.sin(rads) * ship_mc.accel;
ship_mc.xv += xa;
ship_mc.yv += ya;
var speed:Number = Math.sqrt((ship_mc.xv * ship_mc.xv) + (ship_mc.yv * ship_mc.yv));
//trace(speed);
if (speed > 10) {
ship_mc.xv = Math.cos(rads) * 10;
ship_mc.yv = Math.sin(rads) * 10;
}
ship_mc._x += ship_mc.xv;
ship_mc._y += ship_mc.yv;
// Check bounds
if (ship_mc._x > Stage.width ) {
ship_mc._x = 0;
} else if (ship_mc._x < 0) {
ship_mc._x = Stage.width;
}
if (ship_mc._y > Stage.height) {
ship_mc._y = 0;
} else if (ship_mc._y < 0) {
ship_mc._y = Stage.height;
}
}
function onEnterFrame():Void {
if (Key.isDown(Key.UP)) {
ship_mc.accel = 0.5;
} else {
ship_mc.accel = 0;
}
if (Key.isDown(Key.LEFT)) {
ship_mc._rotation -= 10;
}
if (Key.isDown(Key.RIGHT)) {
ship_mc._rotation += 10;
}
moveShip();
checkCollision2(ship_mc);
}
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_mc._x;
b._y = ship_mc._y;
b._rotation = ship_mc._rotation
var rads:Number = degToRad(ship_mc._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);
}
}
}