In my game you need to protect something from enemies. There can be many on the screen at once. But I don't know how to create the enemies randomly, and how I can save the data to each enemy? Can give good tips or good links to tutorials?
4 Answers
In short:
- To generate random numbers use rand()
- To keep track of enemies make arrays/lists of them
Expanded:
You can use the rand() function to generate random numbers. You could use these random numbers to set the position/health points or any other attribute or your enemies.
As for saving the enemies data (I'm assuming you just mean store the variables, correct me if I'm wrong) you need to store the data in an array or list. This can be done in many different ways.
If your enemy is a C-Struct then you can store your enemies in a plain C-Array. If your enemy is implemented as an Objective-C class, then you can store your enemies in a NSArray class (such as NSMutableArray).
Simple thing that you can do is iterate for few times for each enemy generation with the 'rand' function such that the generated values are at a specific distance(MINIMUM_ENEMY_DISTANCE_FROM_PLAYER) from the player.
something like this...
#define SCREEN_WIDTH 480 //just assuming Landscape mode
#define SCREEN_HEIGHT 320
for(i from 1..100) // 100 is some random number-for number of iterations
{
enemyPositionX = arc4random()% SCREEN_WIDTH ;
enemyPositionY = arc4random()% SCREEN_HEIGHT ;
if(distanceToPlayer(enemyPositionX,enemyPositionY) > MINIMUM_ENEMY_DISTANCE_FROM_PLAYER)
{
break;
}
}
//Now use enemyPositionX , enemyPositionY as spawn points for your enemies.
Other option can be... If the enemies generated are moving enemies,you can have some predefined spawnpoints and generate from that positions.You can have a check of this kind of game something like 'Robokill'.
I got a tutorial in my blog about how i made bomb rush that is basically protecting the base from some enemies, it's as3 oriented but the enemies generation concept is the same.
Here is a link to it BliipStudio
Hope it helps you.
If you look through the tutorials at this link, you will find ideas for creating enemies, storing their data, etc.