Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

In short:

  • To generate random numbers use rand()
  • To keep track of enemies make arrays/lists of them


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).

share|improve this answer

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)
   //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'.

share|improve this answer

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.

share|improve this answer

If you look through the tutorials at this link, you will find ideas for creating enemies, storing their data, etc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.