Make your own free website on Tripod.com

The below is the majority of the code used for this tutorial.

The // sign signifies a comment.


//Defining variables that will only be set when the game loads>>
gravity = 0.001;
gravitySpeed = 0;
friction = 0.95;
accelleration = 0.5;
currentXSpeed = 0;
currentYSpeed = 0;
stopY = ground._y;
boundary = 200-character._width;
boundaryUp = ground._y-boundary;
boundaryLeft = ground._x;
boundaryRight = ground._width;
stageHeight = 400;
stageWidth = 550;
frameSpeed = 30;
enemySpawnTimerF = 0;
enemySpawnTimerS = 0;
enemySpeed = 10;
enemySpawnLocationX = redBase._x;
enemySpawnLocationY = redBase._y;
enemyHealth = 50;
gamePaused = false;
attachEnemies = false;
var Enemies;
Enemies = [];
bulletDamage = 25;
shootTimer = 0;
shootDelay = 25;
var Bullets;
Bullets = [];
Text.statusText = "";
currentLevel = 0;
currentRound = 0;
totalMoney = 0;
enemiesAttached = 0;
statusTimer = 0;
stopAttaching = false;
hideText = false;
enableKeys = false;
done1 = false;
done2 = false;
setDistanceX = stageHeight/2;
setDistanceY = stageWidth/2;
var EnemyBullets;
EnemyBullets = [];
blueBaseHealth = 100;
enemyExplodeDamage = 50;
characterHealth = 100;
enemyBulletDamage = 7;
charWidth=character._xscale;
storyline = [["Info Bot", "IB", "Welcome to The attack and defend game. Use the space bar to shoot in the direction you are facing.", "RAG", 0, "false", ""], ["Commander", "C", "Soldier! You're going to need to use the arrow keys and space bar to protect this base, good luck.", "BG", 0, "false", ""], ["Commander", "C", "Attack all the red characters.", "RBG", 0, "false", ""], ["Game", "", "", "", 0, "true", "2"], ["Commander", "C", "Good work soldier! Here's a thousand dollars!", "RAG", 1000, "false", ""], ["Menu", "", "", "", 0, "false", ""], ["Info Bot", "IB", "You have finished the first level. Let's see if you can survive the next!", "BG", 0, "false", ""], ["Commander", "C", "Soldier! You fought through the last attack but to be in the army you will need to withstand a lot more than that. This is the last test round.", "BG", 0, "false", ""], ["Commander", "C", "You fail this and all is lost. Have fun!", "RBG", 0, "false", ""], ["Game", "", "", "", 0, "true", "4"], ["Commander", "C", "Soldier, you are just inspirational. You are now in the army.", "RAG", 2000, "false", ""], ["Info Bot", "IB", "The Commander has awarded you an additional $2000 which you can save up for retirement since there is no shop. You are now in the Army. Objective complete!", "RAG", 0, "false", ""], ["MenuEnd", "", "", "", 0, "false", ""]];

//Defining variables that will only be set when the game loads<<

onEnterFrame = function () {
//on every frame
 Text.swapDepths(root.getNextHighestDepth);
//sets the depth of Text to the next highest depth available
 _y = character._y-(character._y*2)+stageHeight/2;
 _x = character._x-(character._x*2)+stageWidth/2;
//locates the screen around the character
 backgroundPic._y = character._y-stageHeight/2;
 backgroundPic._x = character._x-stageWidth/2;
//locates the background symbol around the character
 ground._x = character._x-stageWidth/2;
//locates the ground symbol around the character
 Text._y = character._y-stageHeight/2;
 Text._x = character._x-stageWidth/2;
//locates the screen around the character
 currentXSpeed *= friction;
 currentYSpeed *= friction;
//multiplies the characters speed by friction
 currentYSpeed += gravitySpeed/2;
//the gravity is added to the current y speed of the character (this will make the character go down)
 multiplyerX = currentXSpeed*-2;
//multiplyerX is used to bounce the character when it hits the sides
 bounceXSpeed = multiplyerX/10;
//uses multiplyerX
 gravitySpeed += gravity;
//gravitySpeed is increased
 character._x += currentXSpeed;
 character._y += currentYSpeed+gravitySpeed;
//moves the character to the speeds
 character._rotation = currentXSpeed*3;
//rotates the character
 enemySpawnTimerF++;
 shootTimer++;
//some timers
 moveEnemy();
//executes the moveEnemy function
 if (character._rotation<=0){
//if the character's rotation is lower than 0
  character._xscale=charWidth*-1;
//the character's xscale equals the reverse of what it was before
 } else {
//if the previous if statement isn't true, do the following...
  character._xscale=charWidth;
//return to what it was before
 }
 if (enemySpawnTimerF>=frameSpeed) {
//if the timer is more than the frame speed...
  attachEnemy();
//execute the attachEnemy function
  enemySpawnTimerS++;
//add onto the enemy spawn timer (seconds speed)
  enemySpawnTimerF = 0;
//reset the enemy spawn timer (frame speed)
 }
 for (var t in Bullets) {
//for every instance in Bullets, let t be used to control each individual Bullet instance (hence the Bullets[t] below)
  Bullets[t]._x += Bullets[t].bulletSpeed;
//for every instance of Bullets, move to it's bullet speed
 }
 for (var i in Enemies) {
//for every instance in Enemies, let i be used to control each individual Enemy instance (hence the Bullets[t] below)
  if (Enemies[i].hitTest(character)) {
//if an individual enemy hits the character
  Enemies[i].enemyWalk = false;
  Enemies[i].enemyAttack = true;
//its enemyWalk boolean value will be set to false, and its Attack boolean value to true
  } else {
//if the previous if statement isn't true, do the following...
  Enemies[i].enemyWalk = true;
  Enemies[i].enemyAttack = false;
//its enemyWalk boolean value will be set to true, and its Attack boolean value to false
  }
  if (Enemies[i].health<=0) {
//if an individual enemy has health equal to or below 0, do the following
  Enemies[i].removeMovieClip();
//remove the enemy
  Text.enemsLeft--;
//lower the number of enemies count
  Enemies.splice(i, 1);
//get rid of the certain enemies from the array
  }
  if (Enemies[i]._x<=blueBase._x) {
//if an individual enemy's x location is past the x location of the bluebase
  Enemies[i].removeMovieClip();
//remove the individual enemy
  Enemies.splice(i, 1);
//get rid of its data
  }
  for (var t in Bullets) {
//for every instance in Bullets, let t be used to control each individual Bullet instance (hence the Bullets[t] below)
  Bullets[t].timer++;
//add to each bullets individual timer
  if (Enemies[i].hitTest(Bullets[t])) {
//if an instance of Enemies hits an instance of Bullets
  Enemies[i].health -= bulletDamage;
//the bulletDamage value is subtracted from the enemy's health
  Bullets[t].removeMovieClip();
//the Bullet is removed
  Bullets.splice(t, 1);
//the bullets data is removed
  }
  if (!Bullets[t].hitTest(backgroundPic)) {
//if the bullet goes out of the screen (since the backgroundPic is always centred on the screen, this is a simple way of detecting if it's still on the screen)
  Bullets[t].removeMovieClip();
//the bullet is removed
  Bullets.splice(t, 1);
/.the bullets data is removed
  }
  }
 }
 if (enableKeys) {
//if the enableKeys boolean is set to true
  if (Key.isDown(Key.SPACE)) {
//if the space bar is down
  if (shootTimer>=shootDelay) {
//if the timer for the characters shooting has exceded the value of shootDelay
  attachBullet();
//do the attachBullet function
  }
  }
  if (Key.isDown(37)) {
//if the left key is being pressed
  currentXSpeed -= accelleration;
//the current x speed is subtracted as much as the value of accelleration
  }
  if (Key.isDown(38)) {
//if the up key is being pressed
  gravitySpeed = 0;
//set the gravity to 0 (this is a better effect then enabling gravity when flying)
  currentYSpeed -= accelleration;
//the current y speed is subtracted (making the character go up)
  }
  if (Key.isDown(39)) {
//if the right key is being pressed
  currentXSpeed += accelleration;
//the current x speed is increased as much as the value of accelleration
  }
  if (Key.isDown(40)) {
//if the down key is being pressed
  currentYSpeed += accelleration;
//the current y speed is increased (making the character go down)
  }
 }
 if (character._y>=stopY) {
//if the characters y position is past or equal to the set stop position
  character._y = stopY;
//the character can't go any further
  if (!Key.isDown(38)) {
//if the up key is being pressed
  gravitySpeed = 0;
  currentYSpeed = 0;
//set the gravity and y speed to 0
  }
 }
 if (character._y<=boundaryUp) {
//if the characters y location is below or equal to boundaryUp
  character._y = boundaryUp;
//the character can't go any further
 }
 if (character._x>=boundaryRight) {
//if the characters x location is equal to or past boundaryRight
  character._x = boundaryRight;
  currentXSpeed += bounceXSpeed;
//x speed is set to 0 and the character can't go any further
 }
 if (character._x<=boundaryLeft) {
//if the characters x location is equal to or past boundaryLeft
  character._x = boundaryLeft;
  currentXSpeed += bounceXSpeed;
//x speed is set to 0 and the character can't go any further
 }
 if (storyline[currentLevel][0] == "Game") {
//if in the storyline array, the level equal to the value of currentLevel, and in the first position equals "Game"
  hideText = true;
//the hideText boolean value is set to true
 } else {
//if anything else...
  hideText = false;
//the hideText boolean value is set to false
 }
 if (storyline[currentLevel][0] == "Menu") {
//if in the storyline array, the level equal to the value of currentLevel, and in the first position equals "Menu"
  nextButton.gotoAndStop(1);
//the nextButton goes to frame one
  showMenu = true;
//the showMenu boolean is set to false
 } else {
//if anything else...
  nextButton.gotoAndStop(2);
//the nextButton goes to the next frame
  showMenu = false;
//the showMenu boolean is set to false
 }
 if (storyline[currentLevel][0] == "MenuEnd") {
//if in the storyline array, the level equal to the value of currentLevel, and in the first position equals "MenuEnd"
  setY = character._y-(character._y*2)+stageHeight/2;
  setX = character._x-(character._x*2)+stageWidth/2;
//the variables used to locate the screen are placed in the original place as before
  gotoAndStop(3);
//the main frame goes to frame 3
  WonOrLost="You win!";
//the display text "WonOrLost", is set to "You win!"
 }
 statusTimer++;
//the statusTimer variable is used to hide the statusText text box after a set time
 if (statusTimer>=90) {
//if the statusTimer variable is more than 90 (with a fps (frames per second) speed of 30, 90 will be 3 seconds
  Text.statusText = "";
//the statusText text box is set to "", so it will display nothing
 }
 Text.menuMc.menuIns.menuBoard.totalMoney = "$"+totalMoney;
//the totalMoney text box inside the menu movieclip is set to the totalMoney variable plus a dollar sign
 if (hideText && !characterHealth<=0 && !blueBaseHealth<=0) {
//if hideText is set to true, character Health doesn't equal 0, and blueBaseHealth doesn't equal 0
  Text.nameText = "";
  Text.textText = "";
//these textboxes are set to true
  Text.charImage.gotoAndStop(1);
//the charImage movieclip is set to go to frame one (which is a blank frame, so it will display nothing)
  Text.shapeScreen._alpha = 0;
//the shapeScreen movieclip is hidden (by changing its transparency to 0)
  enableKeys = true;
  stopAttaching = false;
//the enableKeys and stopAttaching boolean values are set to true and false
  Text.nextButton.gotoAndStop(1);
//the nextButton goes to frame one
  Text.enemsTotal = storyline[currentLevel][6];
//the total enemies that will be attached in the current round are equal to storyline[currentLevel][6], which is a value in the storyline array
  if (!done1) {
//if done1 doesn't equal false
  Text.enemsLeft = storyline[currentLevel][6];
//the enemsLeft textbox is set to storyline[currentLevel][6], a value in the storyline array
  done1 = true;
//done1 is set to true
  }
  if (enemiesAttached>=Text.enemsTotal) {
//if the total number of attached enemies is equal to the total enemies the level was meant to attach
  stopAttaching = true;
//the stopAttaching boolean is set to true
  } else {
//if not...
  stopAttaching = false;
//the stopAttaching boolean is set to false
  }
  if (Text.enemsLeft<=0) {
//if the total enemies left equals 0
  currentLevel++;
//the currentLevel is increased (currentLevel is a variable used to view a particular level of the array
  Text.enemsTotal = "";
  Text.enemsLeft = "";
  Text.statusText = "";
//the above text boxes are set to "" (and will display nothing)
  for (var j in EnemyBullets) {
//for every instance of EnemyBullets let j be used to control every instance of each
  EnemyBullets[j].removeMovieClip();
//remove the instance of EnemyBullet
  EnemyBullets.splice(j, 1);
//remove the EnemyBullets data
  }
  }
 } else {
//if not (referring to the if statement 'if (Text.enemsLeft<=0) {'
  if (showMenu == true) {
//if the showMenu boolean value is set to true
  Text.menuMc.gotoAndStop(2);
//the menuMc (which is the ingame menu) goes to frame two
  Text.nextButton.enabled = false;
//the nextButton is disabled
  if (Text.menuMc.menuIns._currentframe>=Text.menuMc.menuIns._totalframes-1) {
//if the menus animation is on the second to last frame
  Text.menuMc.menuIns.gotoAndStop(Text.menuMc.menuIns._totalframes);
//the menu goes to and stops at the last frame
  }
  } else {
//if not...
  Text.menuMc.gotoAndStop(1);
//the menuMc movieclip goes to frame one
  Text.nextButton.enabled = true;
//the next button is enabled
  }
  if (!characterHealth<=0 && !blueBaseHealth<=0) {
//if character health isn't below 0 and blue base health isn't below 0
  Text.nameText = storyline[currentLevel][0];
  Text.textText = storyline[currentLevel][2];
//set the text boxes to the value of the content of the storyline array
  Text.charImage.gotoAndStop(storyline[currentLevel][1]);
//charImage goes to the frame equal to the value of the storyline array
  }
  Text.shapeScreen._alpha = 50;
//the shapeScreen movie clips alpha is set to 50
  enableKeys = false;
//the enableKeys boolean is set to false
  Text.nextButton.gotoAndStop(2);
//the nextButton movieclip goes to frame 2
 }
 for (var j in Enemies) {
//for every instance of Enemies, let j be used to control every instance of each
  if (Enemies[j].hitTest(blueBase)) {
//if an individual instance of Enemies hits the blue base
  Text.enemsLeft--;
//the text box showing the number of enemies left is reduced
  blueBaseHealth -= enemyExplodeDamage;
//the bluebases health is reduced by the value of enemyExplodeDamage
  Enemies[j].removeMovieClip();
//the instance of Enemies is removed
  Enemies.splice(j, 1);
//the instance of Enemies' data is removed
  }
  var distanceX = character._x-Enemies[j]._x;
  var distanceY = character._y-Enemies[j]._y;
//the distance of each individual character is calculated
  Enemies[j].BulletTimer++;
//the timer of each individual enemy is calculated
  var distanceRadiansX = Math.sqrt(distanceX*distanceX);
  var distanceRadiansY = Math.sqrt(distanceY*distanceY);
//the distance from the characters and the enemies is calculated
  if (distanceRadiansX<setDistanceX && distanceRadiansY<setDistanceY) {
//if the distance is smaller than the set distance
  Enemies[j].enemyWalk = false;
  Enemies[j].enemyAttack = true;
//the enemyWalk and enemyAttack boolean value from each individual enemy is set to false and true
  if (Enemies[j].BulletTimer>=frameSpeed) {
//if the bullet timer of each individual enemy is more or equal to the frameSpeed
  var EnemyBulletHolder = attachMovie("EnemyBullet", "EnemyBullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
//EnemyBulletHolder equals an attached EnemyBullet
  EnemyBulletHolder._x = Enemies[j]._x;
  EnemyBulletHolder._y = Enemies[j]._y-Enemies[j]._height/2;
//the new enemy bullet is located to where the enemy is
  EnemyBulletHolder.angle = Math.atan2((character._y-EnemyBulletHolder._y-character._height/2), (character._x-EnemyBulletHolder._x-character._width/2));
//the angle that the bullet travels is set to the direction the character is from the enemy
  if (Enemies[j]._x<=character._x) {
//if the enemy is past the character
  EnemyBulletHolder.enemyBulletSpeed = 3;
//the bullets speed is set to 3 (this controls your enemy bullet speed)
  } else {
//if not
  EnemyBulletHolder.enemyBulletSpeed = -3;
//the bullets speed is set to -3 (so it will go in the opposite direction)
  }
  EnemyBullets.push(EnemyBulletHolder);
//add this to the EnemyBullets array
  Enemies[j].BulletTimer = 0;
//reset the individual enemies BulletTimer
  }
  } else {
//if not...
  Enemies[j].enemyWalk = true;
  Enemies[j].enemyAttack = false;
//the enemyWalk and enemyAttack boolean values are set to true and false
  }
 }
 for (var j in EnemyBullets) {
//for every individual bullet, let j be used to label them
  EnemyBullets[j]._x += Math.cos(EnemyBullets[j].angle)*5;
  EnemyBullets[j]._y += Math.sin(EnemyBullets[j].angle)*5;
//the bullets travel in the direction and at the speed that they were set
  if (EnemyBullets[j].hitTest(character)) {
//if the individual bullet hits the character
  characterHealth -= enemyBulletDamage;
//the characters health is reduced to the value of enemyBulletDamage
  EnemyBullets[j].removeMovieClip();
//the individual bullet is removed
  EnemyBullets.splice(j, 1);
//the bullets data is removed
  }
  if (EnemyBullets[j].hitTest(blueBase)) {
//if the bullet hits the blue base
  blueBaseHealth -= enemyBulletDamage;
//the blue bases health is reduced to the value of enemyBulletDamage
  EnemyBullets[j].removeMovieClip();
//the enemy bullet is removed
  EnemyBullets.splice(j, 1);
//the data of the enemy bullet is removed
  }
 }
 if (characterHealth<=0 || blueBaseHealth<=0) {
//if the characters health is lower than or equal to zero or if the blue bases health is lower than or equal to zero
  Text.nextButton.gotoAndStop(2);
//the frame of the next button is set to two
  hideText = false;
//the hideText boolean value is set to false
  for (var j in EnemyBullets) {
//for every instance of EnemyBullets, let j be used to label them
  EnemyBullets[j].removeMovieClip();
//remove the instance of EnemyBullets
  EnemyBullets.splice(j, 1);
//remove its data
  }
  for (var j in Enemies) {
//for every instance of Enemies let j be used to label them
  Enemies[j].removeMovieClip();
//the enemy is removed
  Enemies.splice(j, 1);
//the enemies data is removed
  }
  for (var j in Bullets) {
//for every instance of Bullets let j be used to label them
  Bullets[j].removeMovieClip();////////////////////////////////////////////CHANGED ENEMIES TO BULLETS
//remove the bullet
  Bullets.splice(j, 1);////////////////////////////////////////////CHANGED ENEMIES TO BULLETS
//
  }
  enableKeys = false;
//set the enable keys boolean to false
  gotoAndStop(3);
//go to the third frame
  WonOrLost="You lose";
//set the WonOrLost text to "You lose"
 }
 Text.CHealth.gotoAndStop(characterHealth);
 Text.BHealth.gotoAndStop(blueBaseHealth);
//make the health bars go to the health of both the character and blue base
 if (characterHealth<=0) {
//if the characters health is lower than zero
  Text.CHealth.gotoAndStop(1);
//make the characters health go to frame one
 } else {
//if not...
  Text.CHealth.gotoAndStop(characterHealth);
//go to the frame of the value of characterHealth
 }
 if (blueBaseHealth<=0) {
//if blue bases health is lower than or equal to zero
  Text.BHealth.gotoAndStop(1);
//make the blue base health bars 
 } else {
//if not...
  Text.BHealth.gotoAndStop(blueBaseHealth);
//go to the frame of the value of blueBaseHealth
 }
};
function attachEnemy() {
//names and introduces the function
 if (hideText && !stopAttaching) {
//if hideText is true, and stopAttaching is false
  var EnemyHolder = attachMovie("Enemy", "Enemy"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
//EnemyHolder equals a new attached enemy
  enemiesAttached++;
//increases the number of enemies attached
  EnemyHolder._x = enemySpawnLocationX;
  EnemyHolder._y = enemySpawnLocationY;
//locate the enemy to the set spawn location
  EnemyHolder.enemyWalk = true;
//set the enemyWalk boolean to true
  EnemyHolder.enemyAttack = false;
//set the enemyAttack boolean to false
  EnemyHolder.health = enemyHealth;
//sets the enemy health to the enemyHealth variable (that is set onload)
  EnemyHolder.speed = enemySpeed/(random(10)+5);
//the enemy speed is set around the set enemySpeed, and is divided by a random number so that some enemies go faster than others
  EnemyHolder.BulletTimer = 0;
//reset BulletTimer
  Enemies.push(EnemyHolder);
//add the content of EnemyHolder to the Enemies array
  Text.statusText = "Enemy has just spawned.";
//change the statusText text box to "Enemy has just spawned."
  statusTimer = 0;
//reset statusTimer
 }
}
function attachBullet() {
//names and introduces the function
 var BulletHolder = attachMovie("Bullet", "Bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
//BulletHolder equals a new attached bullet
 BulletHolder._x = character._x;
 BulletHolder._y = character._y-character._height/2;
//the bullet is located around the character
 if (currentXSpeed>=0) {
//if the characters x speed is higher that zero
  BulletHolder.bulletSpeed = 10;
//bulletSpeed is set to ten
 } else {
//if not...
  BulletHolder.bulletSpeed = -10;
//bulletSpeed is set to negative ten
 }
 Bullets.push(BulletHolder);
//add BulletHolder onto the Bullets array
 shootTimer = 0;
//shootTimer is reset
}
function moveEnemy() {
//names and introduces the function
 for (var i in Enemies) {
//for every instance of Enemies, let i label them
  if (Enemies[i].enemyWalk) {
//if the boolean value for any instance of Enemies is true
  Enemies[i]._x -= Enemies[i].speed;
//move the enemy at the its individual set speed
  }
 }
}
Text.nextButton.onRelease = function() {
//when the nextButton is clicked
 totalMoney += Text.nameText=storyline[currentLevel][4];
//the total money variable is increased the value of storyline[currentLevel][4]
 currentLevel++;
//the currentLevel variable is increased
 enemiesAttached = 0;
//the enemiesAttached variable is reset
 done1 = false;
//the done1 boolean value is set to true
};