加入敌人的塔防游戏

时间:2011-09-30 00:00:41

标签: flash actionscript-3 actionscript

好的,所以我一直在创建一个教程的塔防游戏。我完成了教程,它工作正常,但我无法弄清楚如何构建它。所以我从头开始建造了更多的塔楼和第二个敌人,但问题是在第一级它可以工作,但在第二级它没有。我一直在用这个工作几个小时我还没找到任何东西。所以我要问的是,如果有人可以给我一个教程或任何可能对阵列和塔防游戏有帮助的东西,它有很多代码,所以我不会发布它,除非你真的需要它

继承教程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();

2 个答案:

答案 0 :(得分:1)

您需要创建轻松重置游戏状态的功能。删除侦听器(如果使用弱引用可能不需要; http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html),清除数组,删除图形(removeChild)等。将所有内容存储在“级别”对象中并具有dispose()(或类似)函数这是一种很好的方式(如评论中所述)。然后只需为下一级创建一个新的关卡对象,你就可以了:)

答案 1 :(得分:1)

我猜你遇到的问题是你没有在开始下一级别之前正确地从关卡中删除所有内容。

以下是“加载游戏”应该如何运作:

  1. 设置所有游戏中的静态资源。 (HUD,Tiles等)
  2. 设置所有塔元素(塔楼购买面板)
  3. 设置所有敌人元素。 (创建代表的数组 将产生的敌人)
  4. 继续设置游戏中的所有其他元素。
  5. 在他们玩完这个级别之后,无论是输赢,都应该删除在“加载”阶段添加的所有内容。

    这里的想法是,你希望加载所有内容,每个新级别,每次执行时都要完全相同,只需对设计级别进行小的更改。像敌人和塔资产这样的东西总是以相同的方式加载,所以你需要确保在下一轮开始之前正确地删除它们,这样当你为那一轮添加它们时,你不会重新添加仍然存在于游戏中。

    你似乎对游戏设计感到困惑。我上面提到的问题只是开始设计游戏时的众多旅行中的一个。我建议你浏览游戏设计书。我推荐this one。我推荐这样一本书的原因是因为它引导您完成游戏设计的概念部分并让您思考正确。我从这本书中学到了游戏编程,最近编码了Symphonic Tower Defense