//----------------------------\ //Main Variables | //----------------------------/ var bulletID:Number = 0; var maxSpeed:Number = 5; var speed:Number = 0; var accel:Number = 0.15; var decel:Number = 0.95; var backSpeed:Number = 0; var maxBackSpeed:Number = -5; var healthPoints:Number = 100; var cheatsOn:Boolean = false; var isMouseDown:Boolean = false; var numBullets:Number = 0; var ammo:Number = 10; var reloadTime:Number = 0; var numBullets:Number = 0; var numEnemies:Number = 0; var bulletList:Array = new Array (); var enemyList:Array = new Array (); var bulletSpeed:Number = 10; var canFire:Boolean = true; var radians:Number = Math.PI/180; var enemySpeed:Number = 2; var turnRate:Number = 0.1; var canSpawn:Boolean = true; var spDelay:Number = 0; var score:Number = 0; //zombies var moveX:Number = 0; var moveY:Number = 0; // Timers var fireTime:Number = 0; var reloadTime:Number = 0; //boundry variables var tankW = Tank._width /2; var tankH = Tank._height /2; var stageW = Stage.width; var stageH = Stage.height; var boundT = 0 + tankH; var boundR = stageW - tankW var boundL = 0 + tankW; var boundB = stageH - 100 - tankH; //sound variables fire = new Sound (); fire.attachSound("Fire") //--------------------------------------------------------- //---------------------------------\ // Find out if the mouse is down | //---------------------------------/ this.onMouseDown = function() //if Mouse mosue is down, set isMouseDown to true { isMouseDown = true; trace("Down") } this.onMouseUp = function() //if Mouse mosue is up, set isMouseDown to false { isMouseDown = false; trace("Up") } //--------------------------------------------------------- //----------------\ // Define Angle | //----------------/ Mouse.hide(); Tank.onEnterFrame = function() { if(canSpawn == true){ // if canSpawn is equal to true, run the code. spawnEnemy() //Run function "spawnEnemy" canSpawn = false // set canSpawn to false so the spawn delay can be triggered } for (i = 0; i <= numEnemies; i++){ if (Tank.hitTest(enemyList[i]) && speed <= -2){ //if player drives over zombies past a set speed, then kill the zombies. enemyList[i].removeMovieClip(); enemyList.splice(i,1); numEnemies--; score+=5 // increase score by 5 } if (Tank.hitTest(enemyList[i]) && speed >= 2){//if player reverses over zombies past a set speed, then kill the zombies. enemyList[i].removeMovieClip(); enemyList.splice(i,1); numEnemies--; score+=5 // increase score by 5 } } if(healthPoints <= 0){ gotoAndStop(2) } mouseCursor.gotoAndPlay(reloadTime) ammoLeft = "Current ammo: " + ammo / 2 // make the text tell us the current ammo sped = "Speed: " + Math.round(speed) // make the text tell us the current speed turRot = "Turret Rotation: "+ Math.round(Tank.turret._rotation); // make the text tell us the current turret rotation tankRot = "Tank Rotation: " + Math.round(Tank._rotation); // make the text tell us the current tank rotation hP = "Health Points: " + Math.round(healthPoints) // make the text tell us the current HP scoreTxt = "Score : " + score // make the text tell us the current score //---------------------------------------------------------- //----------------\ // Define Angle | //----------------/ var __x = _xmouse - Tank._x; var __y = _ymouse - Tank._y; var angle = Math.atan2(__y, __x) * 180 / Math.PI; //make a variable to difine angle so it can be used to rotate the turret //---------------------------------------------------------- mouseCursor._x = _xmouse; //make the crosshair follow the mouse mouseCursor._y = _ymouse; if(Tank._x > boundR){ //if tank xPos is more than the right boundry, tank xPos = boundL Tank._x = boundR; trace("At Right Boundry") } if(Tank._x < boundL){//if tank xPos is less than the left boundry, tank xPos = boundL Tank._x = boundL; trace("At Left Boundry") } if(Tank._y < boundT){ //if tank xPos is less than the left boundry, tank xPos = boundL Tank._y = boundT; trace ("At Top Boundry") } if(Tank._y > boundB){ Tank._y = boundB; trace("At Bottom Boundry") } //-------------------------\ //Character movement code | //-------------------------/ if (Key.isDown(Key.UP))// if the up key has been pressed increase speed by acceleration until it is equal to maxSpeed { trace("Up key is pressed"); if (speed <= maxSpeed){ speed += accel; } }else //else times the speed by decel { speed *= decel; } if(Key.isDown(Key.DOWN)){ //if the down key has been pressed decrease speed by acceleration if(Math.abs(speed) <= maxSpeed){ // Math.abs so the speed can go into negative speed -= accel; } } else{ //else times the speed by decel speed *= decel; } if (Key.isDown(Key.RIGHT))// if the right key is pressed,rotate the tank right. { trace("Right Rotation"); this._rotation += 2; } if (Key.isDown(Key.LEFT))// if the left key is pressed,rotate the tank left. { trace("Left Rotation"); this._rotation -= 2; } //---------------------------------------------------------- //---------------\ // Trigonometry | //---------------/ this._y += Math.sin(Tank._rotation / 180 * Math.PI) * speed;// allows the tank to move forward in the direction it is facing this._x += Math.cos(Tank._rotation / 180 * Math.PI) * speed; //---------------------------------------------------------- //------------------\ //Turret Rotation | //------------------/ Tank.turret._rotation = angle - Tank._rotation; // makes the turret follow the mouse. //---------------------------------------------------------- //--------------------\ // Bullets & reloading| //--------------------/ if (ammo <= 0){ // if ammo is less than or equal to 0, run the "reload" function with the number 100 reload(100); // givees it a reload time so you can't spam shoot. } if(isMouseDown == true){ //if mouse is down,ammo >= 1 and canFire = true, run the "shoot" function,decrease the ammo by 1 and set canFire to false. if (ammo >= 1){// this will place a delay between each fire to make it feel more realisitc if (canFire == true){ shoot() ammo -=1 canFire = false } } } //------------------------------------------------------------------ //----------------\ // Function calls | //----------------/ rotateBullets() fireDelay(40) spawnDelay(60) moveEnemy() deathBox() //------------------------------------------------------------------ }; // onEnterFrame end function deathBox(){ // function to kill bullets when it collides with the black boxes around the map. for(i = 1; i <= numBullets; i++){ if(bulletList[i].hitTest(box1)){ bulletList[i].removeMovieClip(); } if(bulletList[i].hitTest(box2)){ bulletList[i].removeMovieClip(); } if(bulletList[i].hitTest(box3)){ bulletList[i].removeMovieClip(); } if(bulletList[i].hitTest(box4)){ bulletList[i].removeMovieClip(); } } } function spawnEnemy() { numEnemies++; //increase the numEnemies variable by 1 everytime an enemy is spawned. trace("Number of Enemies: " + numEnemies); //tells us how many enemys there are. enemyList[numEnemies] = this.attachMovie("Enemy", "EnemyName" + numEnemies, this.getNextHighestDepth(), { _x:-50 , _y:100}); // spawn enemies at the x cord of -50 so its slightly off the screen. } function moveEnemy(){ if (numEnemies > 0){ //if there are moire than one enemy on the screen. for(num = 1; num <= numEnemies; num++) { doFollow(enemyList[num],Tank,1) //make the enemys walk towards to tank if(Tank.hitTest(enemyList[num])){ //if the enemys hit the tank,decrease health by 0.03 every frame healthPoints-=0.03 } for(i = 1; i <= numBullets; i++) { if(enemyList[num].hitTest(bulletList[i])){ //if bullet hits an enemy remove the bullet enemyList[num].removeMovieClip(); enemyList.splice(num,1); numEnemies--; bulletList[i].removeMovieClip(); score+=10//increase score when an enemy is killed } } } } } function shoot() { numBullets++;//increase the numBullets variable by 1 everytime a bullet is spawned. bulletList[numBullets] = this.attachMovie("Bullet", "bulletName" + numBullets, this.getNextHighestDepth(), { _x:Tank._x , _y:Tank._y}); ammo--; //decrease ammo by one bulletList[numBullets]._rotation = Tank.turret._rotation + Tank._rotation //make the bullet face the direction the turret it rotated to fire.start();//play the souund "fire" when you shoot. } function rotateBullets(){ for(num = 1; num <= numBullets; num++) { bulletList[num]._y += Math.sin(bulletList[num]._rotation / 180 * Math.PI) * bulletSpeed; //makes the bullets travel towards the direction it is facing. bulletList[num]._x += Math.cos(bulletList[num]._rotation / 180 * Math.PI) * bulletSpeed; } } function reload (time:Number){ //1 second = 24,5 seconds = 120. if (reloadTime < time){//if reload is less than time, increase roloadTime every frame and set canFire to false reloadTime ++ canFire = false } if (reloadTime >= time){//if reload is more than or eqial to time, set ammo to 10,set relaodTime to 0 and canFire to true. ammo = 10 reloadTime = 0 canFire = true } } function doFollow(follower:MovieClip, target:MovieClip, enemySpeed:Number) { var originalSpeed:Number = enemySpeed var distanceX:Number = target._x - follower._x;//calculate distance between follower and target var distanceY:Number = target._y - follower._y; distanceTotal = Math.sqrt(distanceX * distanceX + distanceY * distanceY);//get total distance as one number if (target.hitTest(follower)){ enemySpeed = 0; }else{ enemySpeed = originalSpeed; } var moveDistanceX:Number = turnRate * distanceX / distanceTotal;//calculate how much to move var moveDistanceY:Number = turnRate * distanceY / distanceTotal; moveX += moveDistanceX;//increase current speed moveY += moveDistanceY; var totalmove = Math.sqrt(moveX * moveX + moveY * moveY);//get total move distance moveX = enemySpeed * moveX / totalmove;//apply easing moveY = enemySpeed * moveY / totalmove; follower._x += moveX;//move follower follower._y += moveY; follower._rotation = 180 * Math.atan2(moveY, moveX) / Math.PI;//rotate follower toward target } function fireDelay(i:Number){ if (canFire == false){//the function for the fireDelay with chnagable delay times. if (fireTime < i){ fireTime ++ } if (fireTime >= i){//if fireTime is more than or equal to i then fireTime will be reset and canFire will be set to true fireTime = 0 canFire = true } } } function spawnDelay (i:Number){//a fumction to prevent a flood of zombies spawning at once, Changable times to increase or descrease the delay. if (canSpawn == false){ if (spDelay < i){ spDelay++ } } if (spDelay>= i){//if spDelay is more than or equal to i then spDelay will be reset and ccanSpawn will be set to true spDelay = 0 canSpawn = true } }