Posted in:
By Unknown 0 komentar

Membuat Game Tower Defense Bagian 1 (AS2)

Membuat Game Tower Defense




Bagian - Bagian Tutorial

Bagian 1 : Persiapan

       Tower Defense adalah salah satu genre game yang sangat populer selama bertahun - tahun ini. Meskipun dalam pengembangannya game ini cukup sulit untuk dikembangkan. Walaupun begitu, game ini sangat menyenangkan untuk dimainkan. Pada kesempatan kali ini saya akan memandu anda untuk menciptakan sebuah game ber-genre tower defense yang sederhana.
  • Pertama, buat Flash Document ActionScript 2.0 baru berukuran 550px x 400px dengan background berwarna hitam serta kecepatan Frame Per Second (FPS) 24.
  • Buat layer baru diatas layer 1 dan ganti namanya menjadi layer Action.
  •  Masukkan ActionScript berikut kedalam Frame 1 layer Action
 
stop();

//setting vars to step in for turns and special blocks
var S:String = 'START';
var F:String = 'FINISH';
var U:String = 'UP';
var R:String = 'RIGHT';
var D:String = 'DOWN';
var L:String = 'LEFT';

var startDir:String;//the direction the enemies go when they enter
var finDir:String;//the direction the enemies go when they exit
var startCoord:Number;//the coordinates of the beginning of the road
var lvlArray:Array = new Array();//this array will hold the formatting of the roads

lvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,
   0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
   0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
   S,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,
   0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
   0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
   0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   ];

//the names of these variables explain what they do
var currentLvl:Number = 1;
var gameOver:Boolean = false;

function startGame():Void{//we'll run this function every time a new level begins
 //right now we don't have any code
}

_root.createEmptyMovieClip('roadHolder',_root.getNextHighestDepth());//add a MovieClip to hold the road
function makeRoad():Void{
 var row:Number = 0;//the current row we're working on
 var block;//this will act as the block that we're placing down
 for(i=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
  if(lvlArray[i] == 0){//if the current index is set to 0 
   /////*****EMPTY BLOCK*****/////
   _root.createEmptyMovieClip('block'+i,_root.getNextHighestDepth());//create a gray empty block
   _root['block'+i].beginFill(0x333333);
   _root['block'+i].lineTo(0,0);
   _root['block'+i].lineTo(0,25);
   _root['block'+i].lineTo(25,25);
   _root['block'+i].lineTo(25,0);
   _root['block'+i].endFill();
   //and set the coordinates to be relative to the place in the array
   _root['block'+i]._x= (i-row*22)*25;
   _root['block'+i]._y = row*25;
   //giving the block some rollover and rollout effects
   _root['block'+i].onRollOver = function(){
    //Change the color to green
    var newColor = new Color(this);
    newColor.setRGB(0x006600);
   }
   _root['block'+i].onRollOut = function(){
    //Change this color back to gray
    var newColor = new Color(this);
    newColor.setRGB(0x333333);
   }
  } else if(lvlArray[i] == 1){//if there is supposed to be a row
   /////*****EMPTY ROAD*****/////
   //just add a box that will be a darker color and won't have any actions
   roadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());
   roadHolder['block'+i].beginFill(0x111111);
   roadHolder['block'+i].lineTo(0,0);
   roadHolder['block'+i].lineTo(0,25);
   roadHolder['block'+i].lineTo(25,25);
   roadHolder['block'+i].lineTo(25,0);
   roadHolder['block'+i].endFill();
   roadHolder['block'+i]._x= (i-row*22)*25;
   roadHolder['block'+i]._y = row*25;
  } else if(String(lvlArray[i])){//if it's a string, meaning a special block
   /////*****SPECIAL DIRECTIONAL ROAD PIECE*****/////
   roadHolder.createEmptyMovieClip('block'+i,roadHolder.getNextHighestDepth());
   roadHolder['block'+i].beginFill(0x111111);
   roadHolder['block'+i].lineTo(0,0);
   roadHolder['block'+i].lineTo(0,25);
   roadHolder['block'+i].lineTo(25,25);
   roadHolder['block'+i].lineTo(25,0);
   roadHolder['block'+i].endFill();
   roadHolder['block'+i]._x= (i-row*22)*25;
   roadHolder['block'+i]._y = row*25;
  }
  for(var c:Number = 1;c<=16;c++){
   if(i == c*22-1){
    //if 22 columns have gone by, then we move onto the next row
    row++;
   }
  }
 }
}
//run these functions at the start
makeRoad();
startGame();

Preview



Leave a Reply