fOR THIS PROJECT WE HAD TO MAKE A FLASH GAME. i USED THE SOFTWARE aDOBE fLASH CS3 PROFESSIONAL TO MAKE MY TOWER DEFENSE FLASH GAME. THE FIRST TWO TUTORIALS OF MAKING THIS GAME IT FUNCTIONED PROPERLY BUT WHEN I DID THE THIRD AND LAST TUTORIAL OF THE GAME I ADDED IN ALL OF THINGS TO THE GAME JUST HOW THE TUTORIAL SAID TO DO AND THE GAME WOULD NOT WORK FOR ME. iT KEPT SAYING THAT LINE 2 AND 3 "iMPORT FLASH.DISPLAY.*;" AND 'IMPORT FLASH.EVENTS.kEYBOARDEVENT:" ThESE WERE THE SAME LINES I USED TO MAKE THE GAME FROM PARTS 1 AND 2. iT WORKED WITH BOTH PARTS BUT IT GAVE ME SO MUCH FRUSTRATION. FINALLY I JUST DID THE SAME GAME BUT WITH A DIFFERENT TUTORIAL.
The First attempt
package{
import flash.display.*;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.Event; //used for ENTER_FRAME event
public class Main extends MovieClip{
//constants
const monster_startx:int = -15; //starting position the monster
const monster_starty:int = 125;//starting position the monster
const monster_speed:int = 2; //speed of the monster
const monsterno:int = 10; //number of monsters
//variables
var currTower:Tower; //current Tower
var currTile:Tile1; //current Tile
var currGold:int = 100; //current Gold
//Arrays
var nonPlacableTiles:Array = new Array(); //list of tiles on which towers cannot be placed
var level:Array = new Array(); //2D array for representing map
var monsters:Array = new Array(); //list of monsters in the game
var bullets:Array = new Array(); //list of bullets fired
var towers:Array = new Array(); //list of towers placed
var waypoints_x:Array = new Array(); //waypoints for controlling motion of monsters
var waypoints_y:Array = new Array(); //waypoints for controlling motion of monsters
public function Main() {
init();
}
function init():void {
level = [[0,0,0,0,1,1,1,1,1,0],
[0,0,0,0,1,0,0,0,1,0],
[1,1,0,0,1,0,0,0,1,0],
[0,1,0,0,1,0,0,0,1,0],
[0,1,0,0,1,0,0,0,1,0],
[0,1,1,1,1,0,0,0,1,0],
[0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0],
];
//waypoints are points from which the monster must turn
waypoints_x = [75 ,75 ,225,225,425,425,475];
waypoints_y = [125,275,275,25 ,25 ,325,325];
BuildMap(); //function for building the map
for(var i:int=0; i<monsterno; ++i){
createMonster(monster_startx -i*50,monster_starty); //function for creating monsters
}
BtnTower.addEventListener(MouseEvent.MOUSE_UP,TowerBtnUpHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
//this function will build the map
function BuildMap():void{
for(var i:int =0; i < level.length ; ++i){
for(var j:int =0; j < level[i].length ; ++j){ //two loops are needed for accessing elements of a 2D array
var tmpTile:Tile1 = new Tile1();
tmpTile.x = j*50;
tmpTile.y = i*50;
addChild(tmpTile);
tmpTile.gotoAndStop(level[i][j]+1);
if(level[i][j]==1) //add the tile to the nonPlacableTiles list if towers
nonPlacableTiles.push(tmpTile); //cannot be placed on it
tmpTile.addEventListener(MouseEvent.ROLL_OVER,TurnOnTile);
tmpTile.addEventListener(MouseEvent.ROLL_OUT,TurnOfTile);
}
}
}
//this function is called when mouse is rolled over the tile
function TurnOnTile(event:MouseEvent){
currTile = event.currentTarget as Tile1;
if(checkPlacable(currTile) && currTower != null){
currTile.gotoAndStop(3);
}
}
//this function is called when mouse is rolled out of the tile
function TurnOfTile(event:MouseEvent){
var prevTile = event.currentTarget as Tile1;
if(prevTile.currentFrame == 3){
prevTile.gotoAndStop(1);
}
}
//this function creates a monster
function createMonster(xpos:int,ypos:int):void{
var tmpMonster:Monster = new Monster();
tmpMonster.x = xpos;
tmpMonster.y = ypos;
addChild(tmpMonster);
monsters.push(tmpMonster); //add the current monster to the list of monsters
}
//this function checks if a tower can be placed on a tile
function checkPlacable(tmpTile:Tile1):Boolean {
for(var i:int = 0; i<nonPlacableTiles.length ; ++i){
if(nonPlacableTiles[i] == tmpTile)
return false;
}
return true;
}
//this function returns the distance between two movieclips
function distance(A:MovieClip,B:MovieClip):Number{
return Math.pow(Math.pow(B.x-A.x,2) + Math.pow(B.y-A.y,2),0.5);
}
//this function will move a monster in the game (using a loop we can move all monsters)
function moveMonster(tmpMonster:Monster,i:int):void {
var dist_x:Number = waypoints_x[tmpMonster.nextWayPoint] - tmpMonster.x; //distance between the monster
var dist_y:Number = waypoints_y[tmpMonster.nextWayPoint] - tmpMonster.y; //and the nextWayPoint
if(Math.abs(dist_y) + Math.abs(dist_x) < 1){ //increase the nextWayPoint if monster
++tmpMonster.nextWayPoint; //collided with a waypoint
}
var angle:Number = Math.atan2(dist_y,dist_x); //compute the angle of the monster
tmpMonster.x += monster_speed*Math.cos(angle);//update the x position
tmpMonster.y += monster_speed*Math.sin(angle);//update the y position
tmpMonster.rotation = angle/Math.PI*180; //rotate the monster
if(tmpMonster.x >= 475){ //remove the monster if it touches the last wayPoint
removeChild(tmpMonster);
monsters.splice(i,1)
}
if(tmpMonster.hp <= 0){ //remove the monster if its hp becomes 0
currGold += tmpMonster.gold;
removeChild(tmpMonster);
monsters.splice(i,1)
}
}
//this function will update a tower(using a loop we can update all towers)
function updateTowers(tmpTower:Tower):void {
if(tmpTower.reloadTime == 0){
for(var i:int = 0; i < monsters.length ; ++i){ //loop through all the monster
if(distance(tmpTower,monsters[i]) < tmpTower.range){ //check if monster is in the range of the tower
var angle:Number = Math.atan2(monsters[i].y - tmpTower.y, monsters[i].x - tmpTower.x); //compute angle of the bullet
var tmpBullet:Bullet = new Bullet(angle,monsters[i]); // create a bullet
tmpBullet.x = tmpTower.x;
tmpBullet.y = tmpTower.y;
addChild(tmpBullet);
bullets.push(tmpBullet); //add tmpBullet to the list of bullets fired
tmpTower.reloadTime = 30;
break;
}
}
}else{
tmpTower.reloadTime -= 1;
}
}
function onEnterFrameHandler(event:Event){
if(monsters.length <= 0){
gotoAndStop(2);
stage.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
return 0;
}
for(var i:int =0;i< monsters.length;++i){ //update all monsters
moveMonster(monsters[i],i);
}
for(var i:int=0;i<towers.length;++i){ //update all towers
updateTowers(towers[i]);
}
for(var i:int=0;i<bullets.length;++i){ //update all bullets
if(!bullets[i].remove){
bullets[i].update();
}else{
removeChild(bullets[i]);
bullets.splice(i,1);
}
}
if(currTower != null){ // set position of the tower to that of mouse
currTower.x = mouseX + 1 + currTower.width/2;
currTower.y = mouseY + 1 + currTower.height/2;
}
txtGold.text = String(currGold);
}
//this function will create a Tower movieClip if user clicked on Tower Button
function TowerBtnUpHandler(event:MouseEvent){
if(currTower == null){
currTower = new Tower();
addChild(currTower);
}
}
//this function will place the tower when user clicks on the stage
function onMouseDownHandler(event:MouseEvent){
if(currTower != null){
if(checkPlacable(currTile)){ //check if placable
if((currGold-currTower.cost)>=0){ //check if there is enough gold to place the
if(mouseX < 550){ //tower
txtStatus.text = "";
currGold -= currTower.cost;
towers.push(currTower);
currTower.x = currTile.x + currTile.width/2; //place the tower on the tile
currTower.y = currTile.y + currTile.height/2;
currTower = null;
nonPlacableTiles.push(currTile); //add the current Tile into nonPlacableTiles
} //after placing the tower
}else{
txtStatus.text = "not enough gold";
}
}
if(mouseX >500 && mouseX<600){
removeChild(currTower);
currTower = null;
}
}
}
}
}
import flash.display.*;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.Event; //used for ENTER_FRAME event
public class Main extends MovieClip{
//constants
const monster_startx:int = -15; //starting position the monster
const monster_starty:int = 125;//starting position the monster
const monster_speed:int = 2; //speed of the monster
const monsterno:int = 10; //number of monsters
//variables
var currTower:Tower; //current Tower
var currTile:Tile1; //current Tile
var currGold:int = 100; //current Gold
//Arrays
var nonPlacableTiles:Array = new Array(); //list of tiles on which towers cannot be placed
var level:Array = new Array(); //2D array for representing map
var monsters:Array = new Array(); //list of monsters in the game
var bullets:Array = new Array(); //list of bullets fired
var towers:Array = new Array(); //list of towers placed
var waypoints_x:Array = new Array(); //waypoints for controlling motion of monsters
var waypoints_y:Array = new Array(); //waypoints for controlling motion of monsters
public function Main() {
init();
}
function init():void {
level = [[0,0,0,0,1,1,1,1,1,0],
[0,0,0,0,1,0,0,0,1,0],
[1,1,0,0,1,0,0,0,1,0],
[0,1,0,0,1,0,0,0,1,0],
[0,1,0,0,1,0,0,0,1,0],
[0,1,1,1,1,0,0,0,1,0],
[0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0],
];
//waypoints are points from which the monster must turn
waypoints_x = [75 ,75 ,225,225,425,425,475];
waypoints_y = [125,275,275,25 ,25 ,325,325];
BuildMap(); //function for building the map
for(var i:int=0; i<monsterno; ++i){
createMonster(monster_startx -i*50,monster_starty); //function for creating monsters
}
BtnTower.addEventListener(MouseEvent.MOUSE_UP,TowerBtnUpHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
//this function will build the map
function BuildMap():void{
for(var i:int =0; i < level.length ; ++i){
for(var j:int =0; j < level[i].length ; ++j){ //two loops are needed for accessing elements of a 2D array
var tmpTile:Tile1 = new Tile1();
tmpTile.x = j*50;
tmpTile.y = i*50;
addChild(tmpTile);
tmpTile.gotoAndStop(level[i][j]+1);
if(level[i][j]==1) //add the tile to the nonPlacableTiles list if towers
nonPlacableTiles.push(tmpTile); //cannot be placed on it
tmpTile.addEventListener(MouseEvent.ROLL_OVER,TurnOnTile);
tmpTile.addEventListener(MouseEvent.ROLL_OUT,TurnOfTile);
}
}
}
//this function is called when mouse is rolled over the tile
function TurnOnTile(event:MouseEvent){
currTile = event.currentTarget as Tile1;
if(checkPlacable(currTile) && currTower != null){
currTile.gotoAndStop(3);
}
}
//this function is called when mouse is rolled out of the tile
function TurnOfTile(event:MouseEvent){
var prevTile = event.currentTarget as Tile1;
if(prevTile.currentFrame == 3){
prevTile.gotoAndStop(1);
}
}
//this function creates a monster
function createMonster(xpos:int,ypos:int):void{
var tmpMonster:Monster = new Monster();
tmpMonster.x = xpos;
tmpMonster.y = ypos;
addChild(tmpMonster);
monsters.push(tmpMonster); //add the current monster to the list of monsters
}
//this function checks if a tower can be placed on a tile
function checkPlacable(tmpTile:Tile1):Boolean {
for(var i:int = 0; i<nonPlacableTiles.length ; ++i){
if(nonPlacableTiles[i] == tmpTile)
return false;
}
return true;
}
//this function returns the distance between two movieclips
function distance(A:MovieClip,B:MovieClip):Number{
return Math.pow(Math.pow(B.x-A.x,2) + Math.pow(B.y-A.y,2),0.5);
}
//this function will move a monster in the game (using a loop we can move all monsters)
function moveMonster(tmpMonster:Monster,i:int):void {
var dist_x:Number = waypoints_x[tmpMonster.nextWayPoint] - tmpMonster.x; //distance between the monster
var dist_y:Number = waypoints_y[tmpMonster.nextWayPoint] - tmpMonster.y; //and the nextWayPoint
if(Math.abs(dist_y) + Math.abs(dist_x) < 1){ //increase the nextWayPoint if monster
++tmpMonster.nextWayPoint; //collided with a waypoint
}
var angle:Number = Math.atan2(dist_y,dist_x); //compute the angle of the monster
tmpMonster.x += monster_speed*Math.cos(angle);//update the x position
tmpMonster.y += monster_speed*Math.sin(angle);//update the y position
tmpMonster.rotation = angle/Math.PI*180; //rotate the monster
if(tmpMonster.x >= 475){ //remove the monster if it touches the last wayPoint
removeChild(tmpMonster);
monsters.splice(i,1)
}
if(tmpMonster.hp <= 0){ //remove the monster if its hp becomes 0
currGold += tmpMonster.gold;
removeChild(tmpMonster);
monsters.splice(i,1)
}
}
//this function will update a tower(using a loop we can update all towers)
function updateTowers(tmpTower:Tower):void {
if(tmpTower.reloadTime == 0){
for(var i:int = 0; i < monsters.length ; ++i){ //loop through all the monster
if(distance(tmpTower,monsters[i]) < tmpTower.range){ //check if monster is in the range of the tower
var angle:Number = Math.atan2(monsters[i].y - tmpTower.y, monsters[i].x - tmpTower.x); //compute angle of the bullet
var tmpBullet:Bullet = new Bullet(angle,monsters[i]); // create a bullet
tmpBullet.x = tmpTower.x;
tmpBullet.y = tmpTower.y;
addChild(tmpBullet);
bullets.push(tmpBullet); //add tmpBullet to the list of bullets fired
tmpTower.reloadTime = 30;
break;
}
}
}else{
tmpTower.reloadTime -= 1;
}
}
function onEnterFrameHandler(event:Event){
if(monsters.length <= 0){
gotoAndStop(2);
stage.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
return 0;
}
for(var i:int =0;i< monsters.length;++i){ //update all monsters
moveMonster(monsters[i],i);
}
for(var i:int=0;i<towers.length;++i){ //update all towers
updateTowers(towers[i]);
}
for(var i:int=0;i<bullets.length;++i){ //update all bullets
if(!bullets[i].remove){
bullets[i].update();
}else{
removeChild(bullets[i]);
bullets.splice(i,1);
}
}
if(currTower != null){ // set position of the tower to that of mouse
currTower.x = mouseX + 1 + currTower.width/2;
currTower.y = mouseY + 1 + currTower.height/2;
}
txtGold.text = String(currGold);
}
//this function will create a Tower movieClip if user clicked on Tower Button
function TowerBtnUpHandler(event:MouseEvent){
if(currTower == null){
currTower = new Tower();
addChild(currTower);
}
}
//this function will place the tower when user clicks on the stage
function onMouseDownHandler(event:MouseEvent){
if(currTower != null){
if(checkPlacable(currTile)){ //check if placable
if((currGold-currTower.cost)>=0){ //check if there is enough gold to place the
if(mouseX < 550){ //tower
txtStatus.text = "";
currGold -= currTower.cost;
towers.push(currTower);
currTower.x = currTile.x + currTile.width/2; //place the tower on the tile
currTower.y = currTile.y + currTile.height/2;
currTower = null;
nonPlacableTiles.push(currTile); //add the current Tile into nonPlacableTiles
} //after placing the tower
}else{
txtStatus.text = "not enough gold";
}
}
if(mouseX >500 && mouseX<600){
removeChild(currTower);
currTower = null;
}
}
}
}
}
This is the new code i did
package{
//importing required classes for this to work
import flash.display.MovieClip;
import flash.events.*;
public class EmptyBlock extends MovieClip{//defining the class as EmptyBlock
private var _root:MovieClip;//creating a _root variable to access root easily
public function EmptyBlock(){//this function will always run once EmptyBlock is called
this.addEventListener(Event.ADDED, beginClass);//create a function that will run once
this.addEventListener(Event.ENTER_FRAME, eFrameEvents);//create a enterFrame function
}
private function beginClass(e:Event):void{
_root = MovieClip(root);//setting the _root as the root level
this.buttonMode = true;//make this act like a button
this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);//adding function for mouseOver
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);//adding function for mouseOut
this.addEventListener(MouseEvent.CLICK, thisClick);//adding function for clicking
}
private function eFrameEvents(e:Event):void{
if(_root.gameOver){//remove this and listeners if game is over
this.removeEventListener(Event.ENTER_FRAME, eFrameEvents);
this.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);
this.removeEventListener(MouseEvent.CLICK, thisClick);
MovieClip(this.parent).removeChild(this);
}
}
private function thisMouseOver(e:MouseEvent):void{
//changing the background so the user know's it's clickable
this.graphics.beginFill(0x006600);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
_root.rangeCircle.x = this.x;
_root.rangeCircle.y = this.y;
_root.addChild(_root.rangeCircle);
}
private function thisMouseOut(e:MouseEvent):void{
//changing the background back
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
_root.removeChild(_root.rangeCircle);
}
private function thisClick(e:MouseEvent):void{
if(_root.money >= 20){//if the player has enough money
_root.makeTurret(this.x,this.y);//make the turret
//remove all the listeners so it can't be clicked on again
this.buttonMode = false;
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
this.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);
this.removeEventListener(MouseEvent.CLICK, thisClick);
_root.money -= 50; //spend the money
_root.removeChild(_root.rangeCircle);
}
}
}
//importing required classes for this to work
import flash.display.MovieClip;
import flash.events.*;
public class EmptyBlock extends MovieClip{//defining the class as EmptyBlock
private var _root:MovieClip;//creating a _root variable to access root easily
public function EmptyBlock(){//this function will always run once EmptyBlock is called
this.addEventListener(Event.ADDED, beginClass);//create a function that will run once
this.addEventListener(Event.ENTER_FRAME, eFrameEvents);//create a enterFrame function
}
private function beginClass(e:Event):void{
_root = MovieClip(root);//setting the _root as the root level
this.buttonMode = true;//make this act like a button
this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);//adding function for mouseOver
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);//adding function for mouseOut
this.addEventListener(MouseEvent.CLICK, thisClick);//adding function for clicking
}
private function eFrameEvents(e:Event):void{
if(_root.gameOver){//remove this and listeners if game is over
this.removeEventListener(Event.ENTER_FRAME, eFrameEvents);
this.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);
this.removeEventListener(MouseEvent.CLICK, thisClick);
MovieClip(this.parent).removeChild(this);
}
}
private function thisMouseOver(e:MouseEvent):void{
//changing the background so the user know's it's clickable
this.graphics.beginFill(0x006600);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
_root.rangeCircle.x = this.x;
_root.rangeCircle.y = this.y;
_root.addChild(_root.rangeCircle);
}
private function thisMouseOut(e:MouseEvent):void{
//changing the background back
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
_root.removeChild(_root.rangeCircle);
}
private function thisClick(e:MouseEvent):void{
if(_root.money >= 20){//if the player has enough money
_root.makeTurret(this.x,this.y);//make the turret
//remove all the listeners so it can't be clicked on again
this.buttonMode = false;
this.graphics.beginFill(0x333333);
this.graphics.drawRect(0,0,25,25);
this.graphics.endFill();
this.removeEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.removeEventListener(MouseEvent.MOUSE_OUT, thisMouseOut);
this.removeEventListener(MouseEvent.CLICK, thisClick);
_root.money -= 50; //spend the money
_root.removeChild(_root.rangeCircle);
}
}
}
The second try on making the same game it worked and i was really happy. I followed the tutorial exactly and then i modified the code so the map looked a little different and the turents had different colors