_root.attachMovie ("gunbarrell","gunbarrell",_root.getNextHighestDepth());
_root.attachMovie ("levelscreen","levelscreen",_root.getNextHighestDepth());
_root.attachMovie ("mainmenu","mainmenu",_root.getNextHighestDepth());
_root.attachMovie ("gameoverscreen","gameoverscreen",_root.getNextHighestDepth());
_root.attachMovie ("levelcompletescreen","levelcompletescreen",_root.getNextHighestDepth());
_root.attachMovie ("statusbar","statusbar",_root.getNextHighestDepth());
enemy_xspeeds = new Array();
enemy_yspeeds = new Array();
enemy_rspeeds = new Array();
enemy_maxspeeds = new Array();
enemy_hps = new Array(); //current health
enemy_starthps = new Array(); //start health. both needed to get progressbar length.
enemy_IDs = new Array();
bullet_xspeeds = new Array();
bullet_yspeeds = new Array();
bullet_rspeeds = new Array(); //rotation speed
bullet_powers = new Array(); //amount the bullet repels enemies
bullet_hps = new Array(); //amount of damage done by the bullet
bullet_IDs = new Array();
var fade:Boolean;
var fadeitem:String;
var endlevel:Boolean;
gamestate = 0;
currentlevel = 0;
levelcounter = 0;
bl = gunbarrell._height;
fade = false;
fadeitem = "";
gomainmenu();
//button functionality
levelcompletescreen.btnnextlevel.onPress = function(){
if (currentlevel==3){gameover();}else{golevel(currentlevel+1);}
}
gameoverscreen.btnmainmenu.onPress = function(){
gomainmenu();
}
mainmenu.btnnewgame.onPress = function(){
gonewgame();
}
//end buttons
gunbarrell.onEnterFrame = function(){
//this function called every frame
if (gamestate==1){
if (endlevel==true && enemycount==0){
//level complete
endlevel = false;
golevelcomplete();
}
levelautomation();
if (bulletcount > 0){bulletstep();}
if (enemycount > 0){enemystep();}
}
if (fade==true){
if (_root[fadeitem]._alpha > 0){
time = getTimer() - fadetimer;
if (time >= 10){
fadetimer = getTimer();
_root[fadeitem]._alpha -= 10;
}
}else{
fade = false;
//what to do after fade based on fade item
if (fadeitem=="levelscreen"){
gamestate = 1;
levelscreen._y = 2000;
}}}}
function clearscreen(){
//move all movieclips off screen.
for (n=0;n<10;n++){
for (c=0;c<enemycount;c++){killenemy(c);}
}
for (n=0;n<10;n++){
for (c=0;c<bulletcount;c++){killbullet(c);}
}
gunbarrell._y = 2000;
levelscreen._y = 2000;
statusbar._y = 2000;
mainmenu._y = 2000;
gameoverscreen._y = 2000;
levelcompletescreen._y = 2000;
}
function gomainmenu(){
//display main menu
clearscreen();
mainmenu._y = 400;
mainmenu._x = 275;
gamestate = 0;
}
function gonewgame(){
stagexmin = 0; //stage boundaries
stagexmax = 550;
stageymin = 40;
stageymax = 800;
money = 0;
lives = 3;
score = 0;
updatestatus();
golevel(1);
}
function golevel(level:Number){
clearscreen();
gunbarrell._x = 275;
gunbarrell._y = 780;
statusbar._x = 275;
statusbar._y = statusbar._height / 2;
levelscreen._alpha = 100;
levelscreen._y = 400;
levelscreen._x = 275;
levelscreen.txtlevel.text = "Level " + level;
enemycount = 0;
nextenemyID = 0;
bulletcount = 0;
nextbulletID = 0;
levelcounter = 0;
curID = 0;
shottimer = getTimer(); //controls shooting speed
leveltimer = getTimer(); //controls timing of enemies
fadetimer = getTimer(); //controls fade out of various items
reloadspeed = 200; //lower number = faster reload speed
gravity = 0.15;
xfriction = 1; //changed to 1 so that x friction doesn't apply. I left the variable here in case I want to turn it back on.
fade = true;
fadeitem = "levelscreen"
currentlevel = level;
}
function golevelcomplete(){
clearscreen();
levelcompletescreen._x = 275;
levelcompletescreen._y = 400;
levelcompletescreen.txtscore.text = score;
levelcompletescreen.txtmoney.text = money;
levelcompletescreen.txtlives.text = lives;
}
function updatestatus(){
statusbar.txtscore.text = score;
statusbar.txtlives.text = lives;
statusbar.txtmoney.text = money;
}
function levelautomation(){
time = getTimer() - leveltimer;
switch (currentlevel){
case 1: case 2: case 3:
if (levelcounter==0||(levelcounter > 0 && levelcounter < 5 && time >= 3000)){
leveltimer = getTimer();
levelcounter++;
makeenemy(currentlevel);
if (levelcounter==5){
endlevel = true;}
}
break;
}}
function toDeg(Rad:Number){return (Rad * 180 / Math.PI);}
function makebullet(){
//bullet appears at end point of barrell
//bl = barrell length, barrell_rad = barrell angle in radians
bxl = Math.cos(barrell_rad) * bl;
byl = Math.sin(barrell_rad) * bl;
bulletspeed = 10;
//split bulletspeed into an x and y value
ratio = bulletspeed / bl;
bullet_xspeeds[bulletcount] = bxl * ratio;
bullet_yspeeds[bulletcount] = byl * ratio;
bullet_rspeeds[bulletcount] = 10; //rotation speed
bullet_hps[bulletcount] = 5; //damage
bullet_powers[bulletcount] = 4; //repelling power
bullet_IDs[bulletcount] = nextbulletID;
//attach a new instance of bullet from the library with name bullet_1, bullet_2 etc.
bul = _root.attachMovie("bullet","bullet_"+nextbulletID,_root.getNextHighestDepth());
bul.swapDepths(statusbar)
bul._x = gunbarrell._x + bxl; //position the movie clip
bul._y = gunbarrell._y + byl;
nextbulletID++; //increment IDs
bulletcount++; //increment bullet count
}
function bulletstep(){
for (a=0;a<bulletcount;a++){ //step through the array from 0
curID = bullet_IDs[a]
_root['bullet_'+curID]._x += bullet_xspeeds[a]; //apply new positions to
_root['bullet_'+curID]._y += bullet_yspeeds[a]; //relevant bullet on screen
_root['bullet_'+curID]._rotation += bullet_rspeeds[a]; //rotate bullet
bulletcheck(a); //check screen boundaries etc.
}}
function bulletcheck(arraypos:Number){
cx = _root['bullet_'+bullet_IDs[arraypos]]._x; //get x and y position
cy = _root['bullet_'+bullet_IDs[arraypos]]._y;
//check stage boundaries
if (cx < stagexmin || cx > stagexmax || cy < stageymin || cy > stageymax){killbullet(arraypos);}
//check if it hits an enemy
for (b=0;b<enemycount;b++){
if (_root['bullet_'+bullet_IDs[arraypos]].hitTest(_root['enemy_'+enemy_IDs[b]])){
hitenemy(b,arraypos);
}}}
function killbullet(arraypos:Number){
removeMovieClip(_root['bullet_'+bullet_IDs[arraypos]]); //remove the bullet
if (!(arraypos == (bulletcount - 1))){ //if its not the end of the array
for (i = arraypos+1;i<bulletcount;i++){ //then move everything in the array
bullet_xspeeds[i-1] = bullet_xspeeds[i];
bullet_yspeeds[i-1] = bullet_yspeeds[i];
bullet_rspeeds[i-1] = bullet_rspeeds[i];
bullet_powers[i-1] = bullet_powers[i];
bullet_hps[i-1] = bullet_hps[i];
bullet_IDs[i-1] = bullet_IDs[i];
}
}
bulletcount--;
}
function makeenemy(type:Number){
switch(type){
case 1:
enemy_maxspeeds[enemycount] = 4;
enemy_starthps[enemycount] = 20;
size = 40;
break;
case 2:
enemy_maxspeeds[enemycount] = 5.5;
enemy_starthps[enemycount] = 12;
size = 30;
break;
case 3:
enemy_maxspeeds[enemycount] = 2.8;
enemy_starthps[enemycount] = 35;
size = 55;
break;
}
enemy_xspeeds[enemycount] = 0;
enemy_yspeeds[enemycount] = enemy_maxspeeds[enemycount];
enemy_hps[enemycount] = enemy_starthps[enemycount];
enemy_IDs[enemycount] = nextenemyID;
enemy_rspeeds[enemycount] = 0;
en = _root.attachMovie("enemy_"+type,"enemy_"+nextenemyID,_root.getNextHighestDepth());
en.swapDepths(statsubar);
en._x = Math.round(Math.random()*(stagexmax-stagexmin-size))+(size/2);
en._y = stageymin + (size/2);
en._width = size
en._height = size
progressbar = en.attachMovie("progressbar","progressbar",_root.getNextHighestDepth());
progressbar.swapDepths(statusbar);
progressbar._width = 40;
progressbar._height = 6;
nextenemyID++;
enemycount++;
}
function enemystep(){
for (a=0;a<enemycount;a++){
if (enemy_yspeeds[a] < enemy_maxspeeds[a]){enemy_yspeeds[a] += gravity;}
if (enemy_yspeeds[a] > enemy_maxspeeds[a]){enemy_yspeeds[a] = enemy_maxspeeds[a];}
if (enemy_xspeeds[a] != 0){enemy_xspeeds[a] *= xfriction;}
_root['enemy_'+enemy_IDs[a]]._x += enemy_xspeeds[a];
_root['enemy_'+enemy_IDs[a]]._y += enemy_yspeeds[a];
_root['enemy_'+enemy_IDs[a]]._rotation += enemy_rspeeds[a];
enemycheck(a);
}
}
function enemycheck(arraypos:Number){
//make enemy bounce off wales and also detect when enemy goes off the bottom of the screen
ex = _root['enemy_'+enemy_IDs[arraypos]]._x;
ey = _root['enemy_'+enemy_IDs[arraypos]]._y;
er = _root['enemy_'+enemy_IDs[arraypos]]._width / 2;
if (ex < stagexmin+er) {
enemy_xspeeds[arraypos]*=-1;
_root['enemy_'+enemy_IDs[arraypos]]._x = stagexmin+er;
}else if (ex > stagexmax - er){
enemy_xspeeds[arraypos]*=-1;
_root['enemy_'+enemy_IDs[arraypos]]._x = stagexmax - er;
}
if (ey > stageymax - er){ //if off bottom of screen
killenemy(arraypos);
lives--;
updatestatus();
if (lives == 0){
gameover();
}
}
}
function gameover(){
clearscreen();
gameoverscreen._x = 275;
gameoverscreen._y = 400;
gameoverscreen.txtscore.text = score;
gameoverscreen.txtmoney.text = money;
gameoverscreen.txtlevel.text = currentlevel;
gamestate = 0;
}
function hitenemy(enemyarraypos:Number, bulletarraypos:Number){
enemy_hps[enemyarraypos]-=bullet_hps[bulletarraypos];
if (enemy_hps[enemyarraypos]<=0){
money += enemy_starthps[enemyarraypos]/4;
score += enemy_starthps[enemyarraypos];
updatestatus();
killenemy(enemyarraypos);
} else {
score += bullet_hps[bulletarraypos];
updatestatus();
//split the bullets power into x and y vals based on the angle of the bullet to the enemy.
c = Math.sqrt(Math.pow(bullet_xspeeds[bulletarraypos],2) + Math.pow(bullet_yspeeds[bulletarraypos],2));
xforce = bullet_powers[bulletarraypos] * bullet_xspeeds[bulletarraypos] / c;
yforce = bullet_powers[bulletarraypos] * bullet_yspeeds[bulletarraypos] / c;
enemy_xspeeds[enemyarraypos]+=xforce;
enemy_yspeeds[enemyarraypos]+=yforce;
//apply a rotation based on which side of the enemy was struck
if (_root['bullet_'+bullet_IDs[bulletarraypos]]._x < _root['enemy_'+enemy_IDs[enemyarraypos]]._x){
enemy_rspeeds[enemyarraypos] += Math.round(Math.random()*4)+1;
} else {
enemy_rspeeds[enemyarraypos] -= Math.round(Math.random()*4)+1;
}
_root['enemy_'+enemy_IDs[enemyarraypos]].progressbar._width = (enemy_hps[enemyarraypos] / enemy_starthps[enemyarraypos]) * 40;
}
killbullet(bulletarraypos);
}
function killenemy(arraypos:Number){
removeMovieClip(_root['enemy_'+enemy_IDs[arraypos]]);
if (!(arraypos == (enemycount - 1))){
for (i = arraypos + 1;i<enemycount;i++){
enemy_xspeeds[i-1] = enemy_xspeeds[i];
enemy_yspeeds[i-1] = enemy_yspeeds[i];
enemy_rspeeds[i-1] = enemy_rspeeds[i];
enemy_maxspeeds[i-1] = enemy_maxspeeds[i];
enemy_hps[i-1] = enemy_hps[i];
enemy_starthps[i-1] = enemy_starthps[i];
enemy_IDs[i-1] = enemy_IDs[i];
}
}
enemycount--;
}
//mouse functions
onMouseDown = function () { //click
if (gamestate == 1){
timesinceshot = getTimer() - shottimer;
if (timesinceshot >= reloadspeed){
shottimer = getTimer();
makebullet();
}}}
onMouseMove = function () { //move
if (gamestate == 1){
mouse_xdist = _root._xmouse-gunbarrell._x;
mouse_ydist = _root._ymouse-gunbarrell._y;
// calculate the angle
barrell_rad = Math.atan2(mouse_ydist, mouse_xdist);
// convert to degrees and set rotation
barrell_angle = toDeg(barrell_rad) + 90;
gunbarrell._rotation = barrell_angle;
}}
Day 4 - Full Code
Heres the full code from day 4.
Subscribe to:
Comments (Atom)