# Assistance on spawning enemies for a SHMUP game

This topic is 2112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello All,

I am working on my very first game for the iOS platform. Trying to make a SHMUP style game where I am getting to the point on spawning enemies(monsters) to shoot at. The issue that I am having is that the code on spawning every single enemy is getting very lengthy. As the code gets very redundant and repetitive. As in alot of SHMUP games you have multiple of the same type of enemies following each other in a pattern around the screen. For an example this is the code I use to spawn and move a enemy below:

_monsters = [[Monsters alloc] initWithMonsterType:MonsterTypeFlury];

_monsters.position = CGPointMake(self.size.width + _monsters.size.width/2, self.size.height-45);

[_monsters straightLine];

This will spawn a enemy and move it in a straight line from the right side of the screen to the left. Now if I wanted another enemy to come out and follow him I would right that same code again but tell it to wait an additional second or two. I tried using an array format listed below:

#define Num_Monsters 5;

NSMutableArray *monsters = [NSMutableArray arrayWithCapacity:Num_Monsters];

for (int i = 0; i < Num_Monsters; ++i) {

_monsters = [[Monsters alloc]initWithMonsterType:MonsterTypeFlury];

_monsters.position = CGPointMake(self.size.width + _monsters.size.width/2, self.size.height-45);

[_monsters straightLine];

}

But this would spawn the same monster type 5 times at the same exact time so there kinda piled on top of each other... Any help would be great where I don't want to write 4-5 lines of block code for every single enemy that needs to spawn.

Thank you!

bump..

##### Share on other sites

Well, there a lot of ways to slice this problem. You maintain a list of monsters that are "waiting to be spawned", which would contain the parameters necessary to spawn a monster as well as a time delay for when that monster should spawn. Every frame you can decrement the timer on each item of the list, and if the timer is less than zero you spawn a monster with the parameters specified.

Or you could just spawn all five monsters at once, like you're currently doing, but put the monsters that you want to spawn later further back in the world so they don't show up at the same time, even though they were spawned at the same time.

Or you could just simply spawn the monsters at different times. If you want to spawn five monsters at separate times, you could spawn the first monster, then wait until a second has passed before spawning the second, etc. This is similar to the first solution.

My suggestions are kind of vague, but I think you are probably able to fill in most of the details on your own.

##### Share on other sites
It could make your life easier to have a single class that interprets a list of events for generating enemies, instead of you having to hard code the events yourself.
This event list could be a plain text file, with each level having a different one.
Then you parse each event described in the file and store them in an array, sorted by spawning distance so they're easier to use when in-game.

// Enemy spawn script file example.
//
//
// (Enemy Class), (Behavior), (Spawn Distance)

scout, scout_01, 500
scout, scout_01, 510
scout, scout_01, 520
scout, scout_01, 530
scout, scout_01, 540
scout, scout_01, 550 // A series of equally-behavioured enemies a few steps apart, so that they look like an organized line.

cthulhu, cthulhu_01, 700
scout, scout_02, 800
scout, scout_03, 800
interceptor, interceptor_01, 900

...
Similar to how a soft subtitle file works.

- Instead of time-based spawning you can use the "distance travelled" in the level as the reference for spawning the enemies, as it's a simpler logic: An enemy only appears when you've reached a certain point in the level, and this is unrelated to time or framerate.

- The "behaviour" parameter is the choreography that the enemy will perform. It is another script file that defines a catmull-rom spline or similar that describes the path that the enemy will travel, including events such as firing, dropping bombs, how it enters and leaves the screen (position and orientation) etc.
If you design your paths on an abstract 16:9 screen, you can normalize the coordinates and it should work with any screen resolution that you would use with this ratio.

- You can use that script for more than just enemies, like powerups, messages and other types of event. Edited by Kryzon

##### Share on other sites

Yeah currently I do have the monsters spawn over a time period, just a lot of the spawning code gets very redundant. I will have to learn and look into making a script (maybe a plist file) for my monsters on when to spawn and maybe use some sort of behavior on each spawn as well. Kyzon thank you for the example as well I will try to put something together similar.

##### Share on other sites

It seems the issue is simply you're not reusing your code, but rather writing the same lines over and over.  Use functions.  Maybe you need to do some more reading in your language of choice and get a better understanding of it.

I don't know what language you are using, but in pseudo code it would be kind of like this:

while the game is running:
loop through the monsterList:
if monsterList[i] release time is at or past current time:
SpawnMonster([[Monsters alloc] initWithMonsterType:MonsterTypeFlury], monsterList[i].positionX, monsterList[i].positionY)

# SpawnMonster takes a monster and a location to be spawned
SpawnMonster(monster, x, y):
monster.position = CGPointMake(x, y);
[monster straightLine];


• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26