
Added by roScripts on August-25-2007, 2:53 pm
// initialize the score variable
_root.score = 0;
// lives is (obviously) the number of lives you have before your game is over.
_root.lives = 3;
// diffLevel is initialized here.
// it's used for determining the difficulty at each level.
_root.diffLevel = 1;
// Instead of using hitTest, I want to calculate the distance between things.
// When the distance from centerPoint to centerPoint is less than the width and
// height of an object, a collision is detected.
Math.distance = function(x1, y1, x2, y2) {
return msr(mpow(x1-x2, 2)+mpow(y1-y2, 2));
};
// ...and, let's shorten up that command so it performs faster at runtime.
md = Math.distance;
// define the bounds of the movie.
_root.xmax = 390;
_root.xmin = 10;
_root.ymax = 390;
_root.ymin = 10;
showLives = function () {
for (var i = 1; i<=_root.lives; i++) {
_root.panel.createEmptyMovieClip("life"+i, i*30);
_root.panel["life"+i]._x = i*20+60;
_root.panel["life"+i]._y = 50;
_root.panel["life"+i]._xscale = _root.panel["life"+i]._yscale=50;
_root.panel["life"+i].lineStyle(2, 0xcccccc, 100);
_root.panel["life"+i].beginFill(0xffffff, 100);
_root.panel["life"+i].moveTo(0, -15);
_root.panel["life"+i].lineTo(10, 15);
_root.panel["life"+i].lineTo(0, 10);
_root.panel["life"+i].lineTo(-10, 15);
_root.panel["life"+i].lineTo(0, -15);
_root.panel["life"+i].endFill();
}
};
function createPanel() {
_root.createEmptyMovieClip("panel", 12000);
_root.panel._x = 400;
_root.panel._y = 0;
_root.panel.beginFill(0x000000, 100);
_root.panel.lineStyle(1, 0x000000, 100);
_root.panel.moveTo(0, 0);
_root.panel.lineTo(200, 0);
_root.panel.lineTo(200, 400);
_root.panel.lineTo(0, 400);
_root.panel.lineTo(0, 0);
_root.panel.endFill();
// begin text formatting
myformat = new TextFormat();
myformat.font = "Verdana";
myformat.align = "center";
myformat.size = 14;
myformat.bold = true;
myformat.color = 0x00ff00;
// end text formatting
_root.panel.createTextField("scoreboard", 333, 5, 0, 190, 20);
_root.panel.scoreboard.setTextFormat(myformat);
_root.panel.createTextField("message", 222, 0, 325, 200, 20);
_root.panel.message.text = "Good Luck!";
_root.panel.message.setTextFormat(myformat);
_root.panel.createTextField("lev", 444, 0, 100, 200, 20);
_root.panel.lev.text = "level :: "+_root.diffLevel;
_root.panel.lev.setTextFormat(myformat);
_root.panel.createEmptyMovieClip("cont", 111);
_root.panel.cont._x = 0;
_root.panel.cont._y = 370;
_root.panel.cont.beginFill(0x00ff00, 100);
_root.panel.cont.moveTo(10, 0);
_root.panel.cont.lineTo(190, 0);
_root.panel.cont.lineTo(190, 20);
_root.panel.cont.lineTo(10, 20);
_root.panel.cont.lineTo(10, 0);
_root.panel.cont.endFill();
my2format = new TextFormat();
my2format.font = "Verdana";
my2format.align = "center";
my2format.size = 14;
my2format.bold = true;
my2format.color = 0x000000;
_root.panel.cont.createTextField("buttontext", 333, 10, 0, 180, 20);
_root.panel.cont.buttontext.text = ":: click to continue ::";
_root.panel.cont.buttontext.setTextFormat(my2format);
_root.panel.cont.onMouseDown = function() {
if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
Game();
}
};
}
createAsteroid = function (factor, x, y) {
// Let's draw us an asteroid!
var roid = this.createEmptyMovieClip("asteroid"+_root.depth, _root.depth++);
roid.lineStyle(3.5, 0x00ff00, 100);
roid.moveTo(45, -45);
roid.lineTo(90, -15);
roid.lineTo(90, 15);
roid.lineTo(45, 45);
roid.lineTo(0, 15);
roid.lineTo(0, -15);
roid.lineTo(45, -45);
// Let's shorten up some commonly used Math methods to make our
// code execute faster.
var mr = Math.random, mf = Math.floor;
// We need to set a velocity value for the Asteroid's
// x- and y- movements.
roid.xv = mf(mr()*6)-3;
roid.yv = mf(mr()*6)-3;
// We also need to set a rate for rotation of the asteroid,
// so it looks like it's drifting along in space.
roid.r = mf(mr()*360)-180;
// put the Asteroid at the determined x and y on the stage.
roid._x = x;
if (roid._x>20 || roid._x<380) {
roid._y = mf(mr()*400);
} else {
roid._y = y;
}
roid._rotation = mf(mr()*360)-180;
roid._xscale = (mf(mr()*50)+50)/factor;
roid._yscale = (mf(mr()*50)+50)/factor;
roid.level = factor;
return roid;
};
function Game() {
_root.gamePause = false;
_root.gameOver = false;
for (i in _root) {
_root[i].removeMovieClip();
}
// create a control panel
coo = new createPanel();
showLives();
// create a new space ship.
doo = new makeShip();
if (_root.score == 0) {
theScore = "000000";
} else if (_root.score>0 && _root.score<1000) {
theScore = "000"+_root.score;
} else if (_root.score>=1000 && _root.score<10000) {
theScore = "00"+_root.score;
} else if (_root.score>=10000 && _root.score<100000) {
theScore = "0"+_root.score;
} else {
theScore = _root.score;
}
_root.panel.scoreboard.text = "score :: "+theScore;
_root.panel.scoreboard.setTextFormat(myformat);
// at will be an array of object references to the asteroids on the screen.
at = new Array();
// a variable to keep track of the depths of our movie clips is initialized here:
_root.depth = 0;
// let's create a set amount of asteroids depending on the level.
for (i=0; i<_root.diffLevel+1; i++) {
at[i] = new Object();
at[i] = createAsteroid(1, mf(mr()*400), mf(mr()*400));
}
}
// this code will be executed everytime the frame refreshes.
this.onEnterFrame = function() {
var i = at.length;
if (i == 0){
if (!_root.gamePause){
_root.gamePause = true;
_root.panel.message.text = "Level Complete!";
_root.diffLevel++;
_root.panel.lev.text = "level :: " + _root.diffLevel;
_root.panel.lev.setTextFormat(myformat);
_root.panel.message.setTextFormat(myformat);
}
}
for (i in at) {
if (at[i]._x<=_root.xmin+10) {
at[i]._x += _root.xmax-20;
}
if (at[i]._x>=_root.xmax-10) {
at[i]._x -= _root.xmax-20;
}
if (at[i]._y<=_root.ymin+10) {
at[i]._y += _root.ymax-20;
}
if (at[i]._y>=_root.ymax-10) {
at[i]._y -= _root.ymax-20;
}
at[i]._x += at[i].xv*at[i].level;
at[i]._y += at[i].yv*at[i].level;
at[i]._rotation += at[i].r/60;
// Get the distance between each asteroid and the ship,
// and check if that distance is less than the half the width of
// each asteroid.
if (md(at[i]._x, at[i]._y, ship._x, ship._y)<at[i]._width/2) {
// we want a gamePause to stop the game operations if the
// ship hits the asteroid.
if (!_root.gamePause) {
_root.panel["life"+_root.lives]._visible = 0;
_root.lives--;
_root.gamePause = true;
ship.removeMovieClip();
if (_root.lives == 0) {
_root.panel.message.text = "Game Over, dude.";
_root.gameOver = true;
_root.score = 0;
_root.diffLevel = 1;
_root.lives = 3;
} else {
_root.panel.message.text = "OUCH! That hurt!";
}
_root.panel.message.setTextFormat(myformat);
}
}
// we'll perform the same operations for every object reference
// in the laser array
for (b in lasers) {
var roid = at[i];
var bull = lasers[b];
if (md(roid._x, roid._y, bull._x, bull._y)<roid._width/2) {
at.splice(i, 1);
if (!_root.gameOver) {
_root.score += 200;
if (_root.score<1000) {
theScore = "000"+_root.score;
} else if (_root.score>=1000 && _root.score<10000) {
theScore = "00"+_root.score;
} else if (_root.score>=10000 && _root.score<100000) {
theScore = "0"+_root.score;
} else {
theScore = _root.score;
}
}
_root.panel.scoreboard.text = "score :: "+theScore;
_root.panel.scoreboard.setTextFormat(myformat);
if (roid.level<3) {
roid.level++;
at.push(createAsteroid(roid.level, roid._x, roid._y));
at.push(createAsteroid(roid.level, roid._x, roid._y));
}
_root.lasers.splice(b, 1);
void (bull.removeMovieClip());
void (roid.removeMovieClip());
return;
}
}
}
};
function makeShip() {
createEmptyMovieClip("ship", 10000);
ship._x = ship._y=200;
ship._xscale = ship._yscale=50;
with (ship) {
lineStyle(2, 0xcccccc, 100);
beginFill(0xffffff, 100);
moveTo(0, -15);
lineTo(10, 15);
lineTo(0, 10);
lineTo(-10, 15);
lineTo(0, -15);
endFill();
}
ship.angle = 0;
// angle is the ship's rotation angle.
ship.xv = 0;
// xv is the ship's x=velocity (going across)
ship.x = 0;
// x is the ship's x-position.
ship.y = 0;
// y is the ship's y-position.
ship.yv = 0;
// yv is the ship's y-velocity (going up/down)
ship.xa = 0;
// xa is the ship's x-acceleration (how it speeds up/slows down)
ship.ya = 0;
// ya is the ship's y-acceleration (how it speeds up/slows down)
ship.v = 0;
// v is the ship's "vector"
ship.visc = 0.02;
// the ship has a "viscocity" factor to slow down some response time.
ship.drag = 0;
// drag will use the viscocity factor
// Let's shorten up more Math functions.
mp=Math.PI, mpow=Math.pow, msr=Math.sqrt;
// We're going to put a timer in for the firing engine, so we don't fire all our
// lasers at the same time - spacing out the firing to make better shooting.
ship.timer = 0;
// We'll set up an array of object references to each laser.
lasers = new Array();
// Everytime the frame refreshes, all these operations are going to execute.
ship.onEnterFrame = function() {
if (this.timer>0) {
this.timer--;
}
if (Key.isDown(Key.SPACE) && this.timer == 0) {
this.timer = 6;
// BEGIN firing -- lock and load :)
if (_root.lasers.length<5) {
lasers.push(_root.createEmptyMovieClip("f"+_root.depth, _root.depth++));
var id = lasers.length-1;
var laser = lasers[id];
laser.id = id;
laser.lineStyle(1, 0xcccccc, 100);
laser.beginFill(0xcccccc, 100);
laser.moveTo(-2, -2);
laser.lineTo(2, -2);
laser.lineTo(2, 2);
laser.lineTo(-2, 2);
laser.lineTo(-2, -2);
laser.endFill();
laser.angle = this.angle;
laser.alive = 20;
// alive is what we use so your lasers don't continue perpetually.
laser._x = this._x;
laser._y = this._y;
laser.onEnterFrame = function() {
if (this.alive>0) {
this.alive--;
this._y += -15*Math.cos(mp/180*this.angle);
this._x += 15*Math.sin(mp/180*this.angle);
if (this._x<=_root.xmin) {
this._x += _root.xmax-10;
}
if (this._x>=_root.xmax) {
this._x -= _root.xmax-10;
}
if (this._y<=_root.ymin) {
this._y += _root.ymax-10;
}
if (this._y>=_root.ymax) {
this._y -= _root.ymax-10;
}
} else {
for (var k in _root.lasers) {
if (_root.lasers[k] == this) {
_root.lasers.splice(k, 1);
}
}
this.removeMovieClip();
}
};
}
}
// Begin all the movement here.
// RIGHT and LEFT control the amount of torque applied to the ship's movement
if (Key.isDown(Key.RIGHT)) {
this.torque = 5;
} else if (Key.isDown(Key.LEFT)) {
this.torque = -5;
} else {
this.torque = 0;
}
this.rtorque = 0.5*this.AngVelocity;
this.AngAcceleration = this.torque-this.rtorque;
this.AngVelocity += this.AngAcceleration;
this.angle += this.AngVelocity;
// constrain theta 0 < 360
// We only want to deal with angles from 0 to 360 degrees
if (this.angle<0) {
this.angle += 360;
}
if (this.angle>360) {
this.angle -= 360;
}
this.v = msr(mpow(this.xv, 2)+mpow(this.yv, 2));
this.drag = this.visc*this.v;
// Here we control the thrust and acceleration of the ship
if (Key.isDown(Key.UP)) {
this.force = 1;
} else {
this.force = 0;
}
this.xa = ((this.drag*this.xv)*-1)+(this.force*Math.sin(mp/180*this.angle));
this.ya = ((this.drag*this.yv)*-1)-(this.force*Math.cos(mp/180*this.angle));
this.xv += this.xa;
this.yv += this.ya;
// Just like the asteroids, we need to constrain the ship within
// the bounds of the playable area.
if (this._x<=_root.xmin) {
this._x += _root.xmax-10;
}
if (this._x>=_root.xmax) {
this._x -= _root.xmax-10;
}
if (this._y<=_root.ymin) {
this._y += _root.ymax-10;
}
if (this._y>=_root.ymax) {
this._y -= _root.ymax-10;
}
// And here we finally assign and update the ship's x- and
// y- positions, and it's rotation angle.
this._y += this.yv;
this._x += this.xv;
this._rotation = this.angle;
// ... END movement
};
}
foo = new Game();
Added by roScripts on March-26-2008, 5:14 pm
Added by roScripts on March-26-2008, 5:13 pm
Added by roScripts on March-26-2008, 5:09 pm
Added by roScripts on March-26-2008, 5:08 pm
Added by roScripts on March-26-2008, 5:07 pm