2011-09-30 190 views
0

好吧,所以我一直在創建教程的塔防遊戲。我完成了教程,它運行良好,但我無法弄清楚如何建立它。所以我從頭開始,建造了更多的塔樓和第二個敵人,但問題在於它在第一級工作,但在第二級則沒有。我一直在這工作幾個小時,我還沒有找到任何東西。所以我問,如果有人可以給我一個教程或任何可能對as3陣列和塔防遊戲的幫助,它是一種很多代碼,所以我現在不會發布它,除非你真的需要它as3中的塔防遊戲添加敵人

繼承人教程http://www.flashgametuts.com/tutorials/as3/how-to-create-a-tower-defense-game-in-as3-part-1/

stop(); 
//othervariables 
var money:int=100;//how much money the player has to spend on turrets 
var lives:int=20;//how many lives the player has 
//lvlarray 
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:int;//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, 
      ]; 
//enemy1array 
var currentLvl:int = 1; 
var gameOver:Boolean = false; 

var currentEnemy:int = 0;//the current enemy that we're creating from the array 
var enemyTime:int = 0;//how many frames have elapsed since the last enemy was created 
var enemyLimit:int = 12;//how many frames are allowed before another enemy is created 
var enemyArray:Array = new Array();//this array will tell the function when to create an enemy 
var enemiesLeft:int;//how many enemies are left on the field 
enemyArray = [//defining the array 
      [2,2,1,1,1],//1's will just represent an enemy to be created 
      [1,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//another row means another level 
      [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] 
       ]; 

function startGame():void{//we'll run this function every time a new level begins 
    for(var i:int=0;i<enemyArray[currentLvl-1].length;i++){ 
     if(enemyArray[currentLvl-1][i] == 1){ 
      enemiesLeft ++; 
     } 
    } 
} 
//enemy2array 
var currentEnemy2:int = 0;//the current enemy that we're creating from the array 
var enemy2Time:int = 0;//how many frames have elapsed since the last enemy was created 
var enemy2Limit:int = 15;//how many frames are allowed before another enemy is created 


function start2Game():void{//we'll run this function every time a new level begins 
    for(var i:int=0;i<enemyArray[currentLvl-1].length;i++){ 
     if(enemyArray[currentLvl-1][i] == 1){ 
      enemiesLeft ++; 
     } 
    } 
} 
//lvlcreate 
var roadHolder:Sprite = new Sprite();//create an object that will hold all parts of the road 
addChild(roadHolder);//add it to the stage 
function makeRoad():void{ 
    var row:int = 0;//the current row we're working on 
    var block;//this will act as the block that we're placing down 
    for(var i:int=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 
      block = new EmptyBlock();//create a gray empty block 
      block.graphics.beginFill(0x333333); 
      block.graphics.drawRect(0,0,25,25); 
      block.graphics.endFill(); 
      addChild(block); 
      //and set the coordinates to be relative to the place in the array 
      block.x= (i-row*22)*25; 
      block.y = row*25; 
     } else if(lvlArray[i] == 1){//if there is supposed to be a row 
      //just add a box that will be a darker color and won't have any actions 
      block = new Shape(); 
      block.graphics.beginFill(0x111111); 
      block.graphics.drawRect(0,0,25,25); 
      block.graphics.endFill();  
      block.x= (i-row*22)*25; 
      block.y = row*25; 
      roadHolder.addChild(block);//add it to the roadHolder 
     } else if(lvlArray[i] is String){//if it's a string, meaning a special block 
      //then create a special block 
      block = new DirectBlock(lvlArray[i],(i-row*22)*25,row*25); 
      addChild(block); 
     } 
     for(var c:int = 1;c<=16;c++){ 
      if(i == c*22-1){ 
       //if 22 columns have gone by, then we move onto the next row 
       row++; 
      } 
     } 
    } 
} 
//towers 
function makeTurret(xValue:int,yValue:int):void{//this will need to be told the x and y values 
    var turret:Turret = new Turret();//creating a variable to hold the Turret 
    //changing the coordinates 
    turret.x = xValue+12.5; 
    turret.y = yValue+12.5; 
    addChild(turret);//add it to the stage 
} 

function makeTurret2(xValue:int,yValue:int):void{//this will need to be told the x and y values 
    var turret2:Turret2 = new Turret2();//creating a variable to hold the Turrettwo 
    //changing the coordinates 
    turret2.x = xValue+12.5; 
    turret2.y = yValue+12.5; 
    addChild(turret2);//add it to the stage 
} 
//enemy1 
addEventListener(Event.ENTER_FRAME, eFrame);//adding an eFrame function 
function eFrame(e:Event):void{ 
    //if there aren't any levels left 
if(currentLvl > enemyArray.length){ 
    gameOver=true;//set the game to be over 

    //reset all the stats 
    currentLvl = 1; 
    currentEnemy = 0; 
    enemyTime = 0; 
    enemyLimit = 12; 
    enemiesLeft = 0; 

    removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener 
    removeChild(roadHolder);//remove the pieces of road 
    gotoAndStop('win');//go to the win frame 
} 
if(lives<=0){//if the user runs out of lives 
    gameOver=true;//set the game to be over 

    //reset all the stats 
    currentLvl = 1; 
    currentEnemy = 0; 
    enemyTime = 0; 
    enemyLimit = 12; 
    enemiesLeft = 0; 

    removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener 
    removeChild(roadHolder);//remove the pieces of road 
    gotoAndStop('lose');//go to the lose frame 
} 
    makeEnemies();//we'll just make some enemies 
    if(enemiesLeft==0){//if there are no more enemies left 
    currentLvl ++;//continue to the next level 
    currentEnemy = 0;//reset the amount of enemies there are 
    start2Game(); 
    startGame();//restart the game 
} 
//Updating the text fields 
txtLevel.text = 'Level '+currentLvl; 
txtMoney.text = '$'+money; 
txtLives.text = 'Lives: '+lives; 
txtEnemiesLeft.text = 'Enemies Left: '+enemiesLeft; 

} 

function makeEnemies():void{//this function will add enemies to the field 
    if(enemyTime < enemyLimit){//if it isn't time to make them yet 
     enemyTime ++;//then keep on waiting 
    } else {//otherwise 
     var theCode:int = enemyArray[currentLvl-1][currentEnemy];//get the code from the array 
     if(theCode == 2){//if it's set as 1 
      var newEnemy:Enemy = new Enemy();//then create a new enemy 
      enemyHolder.addChild(newEnemy);//and add it to the enemyholder 
     } 
     currentEnemy ++;//move on to the next enemy 
     enemyTime = 0;//and reset the time 
    } 
} 
//enemy2 
addEventListener(Event.ENTER_FRAME, e2Frame);//adding an eFrame function 
function e2Frame(e:Event):void{ 
    //if there aren't any levels left 
if(currentLvl > enemyArray.length){ 
    gameOver=true;//set the game to be over 

    //reset all the stats 
    currentLvl = 1; 
    currentEnemy2 = 0; 
    enemy2Time = 0; 
    enemy2Limit = 12; 
    enemiesLeft = 0; 

    removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener 
    removeChild(roadHolder);//remove the pieces of road 
    gotoAndStop('win');//go to the win frame 
} 
if(lives<=0){//if the user runs out of lives 
    gameOver=true;//set the game to be over 

    //reset all the stats 
    currentLvl = 1; 
    currentEnemy2 = 0; 
    enemy2Time = 0; 
    enemy2Limit = 12; 
    enemiesLeft = 0; 

    removeEventListener(Event.ENTER_FRAME, e2Frame);//remove this listener 
    removeChild(roadHolder);//remove the pieces of road 
    gotoAndStop('lose');//go to the lose frame 
} 
    makeEnemies2();//we'll just make some enemies 

    if(enemiesLeft==0){//if there are no more enemies left 
    currentLvl ++;//continue to the next level 
    currentEnemy2 = 0;//reset the amount of enemies there are 
    start2Game(); 
    startGame();//restart the game 
} 
    //Updating the text fields 
txtLevel.text = 'Level '+currentLvl; 
txtMoney.text = '$'+money; 
txtLives.text = 'Lives: '+lives; 
txtEnemiesLeft.text = 'Enemies Left: '+enemiesLeft; 

} 

function makeEnemies2():void{//this function will add enemies to the field 
    if(enemy2Time < enemy2Limit){//if it isn't time to make them yet 
     enemy2Time ++;//then keep on waiting 
    } else {//otherwise 
     var theCode:int = enemyArray[currentLvl-1][currentEnemy2];//get the code from the array 
     if(theCode == 2){//if it's set as 1 
      var newEnemy2:Enemy2 = new Enemy2();//then create a new enemy 
      enemyHolder.addChild(newEnemy2);//and add it to the enemyholder 
     } 
     currentEnemy2 ++;//move on to the next enemy 
     enemy2Time = 0;//and reset the time 
    } 
} 
//other 
//run these functions at the start 
makeRoad(); 
var enemyHolder:Sprite = new Sprite(); 
addChild(enemyHolder); 
startGame(); 
start2Game(); 
+0

在遊戲開發中最困難的事情之一就是確保**沒有**被留在/從上一級溢出。最好的辦法是有一個關卡類,它包含所有敵人和東西的數組,並且包含'dispose()'函數或類似的東西。每當您編寫將數據添加到關卡的代碼時,請確保更新'dispose()'函數以刪除此信息。 – Marty

+0

我想我有那個但是...... – Thor625

+1

是的,這很痛苦。可以自己做一些正義的東西來看待爲你處理這些東西的遊戲引擎,比如PushButton。由於我使用自己的引擎,所以並沒有多少關注它,但顯然它確實不錯:http:// pushbuttonengine。com/ – Marty

回答

1

您需要創建能夠輕鬆地重置您的遊戲狀態。刪除偵聽器(如果使用弱引用,則可能不需要; http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html),清除數組,刪除圖形(removeChild)等。將所有內容存儲在「level」對象中並在其上具有dispose()(或類似)函數是一個很好的這樣做的方式(如評論中所述)。然後,簡單地創建一個新的關卡對象爲下一個級別,你很好去:)

1

我想你遇到的問題是,你沒有從層面上正確地刪除所有的東西,然後開始下一個級別。

這裏是如何的「遊戲加載」應該工作:

  1. 設置所有遊戲的靜態資產。 (HUD,瓷磚等)
  2. 設置所有塔元素(塔的購買面板)
  3. 設置所有敵方元素。 (創建代表 將會產生的敵人的陣列)
  4. 繼續設置遊戲中的所有其他元素。

當他們完成了這個關卡之後,無論是輸贏,你應該刪除所有在「加載」階段添加的東西。

這裏的想法是,你希望一切都被加載,每一個新的水平,每次你做一個像設計一樣的小改變時完全相同的方式。敵人和塔式資產的東西總是以同樣的方式加載,所以你需要確保在下一輪開始之前將它們正確地移除,這樣當你爲這一輪添加它們時,你不會重新添加仍然存在的東西存在於遊戲中。

你對一般的遊戲設計感到困惑。上面提到的問題只是開始設計遊戲時的諸多挑戰之一。我建議你閱讀一本遊戲設計書。我推薦this one。我推薦這樣一本書的原因是因爲它會引導你瀏覽遊戲設計的概念部分,並讓你思考正確。我從這本書中學習了遊戲編程,最近編碼爲Symphonic Tower Defense