Question

I found a website, http://www.flashgametuts.com/tutorials/as3/how-to-create-a-tower-defense-game-in-as3-part-1/, that gave me a basic tutorial on how to create a Tower Defense game in action script 3.0 and the way that it said to put the creation of enemies in an array and the number you put would mean the enemy's level and sub arrays would be for different levels:

enemyArray = [//defining the array
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//#s will just represent an enemy to be created
        [1,1,1,1,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],
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,2,2,2,2,2,0,0,0,0,3,3,3,3,3,3,3,50]
          ];

And my question was "is there a way to have it automatically generate levels so that it is like a survival without an end? if so how"

In the end I don't necessarily care if this array method is what is still used or not but your help is greatly appreciated.

note: if you follow the link to the tutorial I found many instances where the coding there was incorrect and caused fatal errors.

Was it helpful?

Solution

I think what you are asking for is rather simplistic in concept.

For example if your enemy levels are 0-50, you can just have it create a new array with randomized numbers. I would think that you'd want to do it a little more intelligently and take into account the enemy strength, spawn order etc.

If you wanted to create a random level you could have a function to return an array containing a specified amount of random enemies in a level range :

function getLevelSpawns(amount:int, levelMin:int, levelMax:int):Array
{
    var spawn:Array = new Array;
    for (var index:int = 0;index < amount;index++)
    {
         var enemyType:int = Math.random() * (levelMax-levelMin) + levelMin;
         spawn.push(enemyType);
    }

   return spawn;
}

and the usage would be :

var spawns:Array = getLevelSpawns(50, 5,10);

That would give you a random spawn array of 50 enemies of level 5 - 10.

This is just an example. You can make that getLevelSpawns function more flexible and robust to handle grouping them and being more intelligent about creating a group via parameters and logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top