Public Group

# Using arrays to create enemies...

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

## Recommended Posts

[font="arial, sans-serif"]Ive recently begun creating arrays to use for my enemy ships. I thought this was most appropriate because there could be upto 100 on the screen at a time.[/font]
[font="arial, sans-serif"]i was wondering if i allocate an array with say [100] ; and i want to use 0-10 spaces of that array to perhaps be drawn at 200,300 , then 10-20 to be drawn at 300,400 how [/font]
[font="arial, sans-serif"]would i go about doing this.[/font]
[font="arial, sans-serif"]If your wondering why i want to use this is , its because im doing a side scroller shoot 'em up and the enemies need patterns.[/font]

[font="arial, sans-serif"]Heres an example of it working...[/font]
[font="arial, sans-serif"]This code creates one of enemies every 200 spaces across the X axis.[/font]
[font="arial, sans-serif"]As you can see im only allocating a maximum of 20 spaces for my array anyway.[/font]
[font="arial, sans-serif"]I could recreate a seperate array which will draw the enemies elsewhere but i think this would be overcomplicating things and just make the job tedious.[/font]

[font="arial, sans-serif"]
aEnemy = new Enemy[20];
//aEnemyWave2 = new Enemy[20];

for (int i = 0; i < 10; i++)
{
aEnemy = new Enemy(500 + (i * 200), 200, badShip);

}

for (int i = 10; i < 20; i++)
{
aEnemy = new Enemy(500 + (i * 200), 400, badShip);

}[/font]

##### Share on other sites

[font="arial, sans-serif"] i want to use 0-10 spaces of that array to perhaps be drawn at 200,300 , then 10-20 to be drawn at 300,400 how [/font]
[font="arial, sans-serif"]would i go about doing this.[/font]

First of all, in the future, surround your blocks of code with [ code ] put your code here [ /code ], this will make it look a lot cleaner.

If you want to set ten spaces of an array to the same value, you could do something like this:

 int i; Enemy aEnemy[20]; for (i = 0; i < 20; i++) { if (i < 10) { aEnemy = new Enemy(200, 300, badship); } else { aEnemy = new Enemy(200, 400, badship); } } 

However, this will spawn ten enemies in the exact same location ((200, 300) and (200, 400)). Is this what you are trying to do?

A cool pattern might look like this:

 for (i = 0; i < 20; i++) { if (i < 10) { aEnemy = new Enemy(200 + 100 * i, 400 - 20 * i, badship); } else { aEnemy = new Enemy(200 + 100 * i, 100 + 20 * i, badship); } } 

This way, your twenty enemies will have the following coordinates:
(200, 400)
(300, 380)
(400, 360)
(500, 340)
(600, 320)
(700, 300)
(800, 280)
(900, 260)
(1000, 240)
(1100, 220)
(1200, 100)
(1300, 120)
(1400, 140)
(1500, 160)
(1600, 180)
(1700, 200)
(1800, 220)
(1900, 240)
(2000, 260)
(2100, 280)

##### Share on other sites

[font="arial, sans-serif"]Ive recently begun creating arrays to use for my enemy ships. I thought this was most appropriate because there could be upto 100 on the screen at a time.[/font]
[font="arial, sans-serif"]i was wondering if i allocate an array with say [100] ; and i want to use 0-10 spaces of that array to perhaps be drawn at 200,300 , then 10-20 to be drawn at 300,400 how [/font]
[font="arial, sans-serif"]would i go about doing this.[/font]
[font="arial, sans-serif"]If your wondering why i want to use this is , its because im doing a side scroller shoot 'em up and the enemies need patterns.[/font]

[font="arial, sans-serif"]Heres an example of it working...[/font]
[font="arial, sans-serif"]This code creates one of enemies every 200 spaces across the X axis.[/font]
[font="arial, sans-serif"]As you can see im only allocating a maximum of 20 spaces for my array anyway.[/font]
[font="arial, sans-serif"]I could recreate a seperate array which will draw the enemies elsewhere but i think this would be overcomplicating things and just make the job tedious.[/font]

[font="arial, sans-serif"]
aEnemy = new Enemy[20];
//aEnemyWave2 = new Enemy[20];

for (int i = 0; i < 10; i++)
{
aEnemy = new Enemy(500 + (i * 200), 200, badShip);

}

for (int i = 10; i < 20; i++)
{
aEnemy = new Enemy(500 + (i * 200), 400, badShip);

}[/font]

This code will work if i can assume that aEnemy is defined as Enemy ** aEnemy(assuming this is written in C++) but it is easier to write, read and extend if it is written like this:
 for(int counter = 0; counter < 20; ++counter) { if (counter < 10) { aEnemy = new Enemy(500 + (i * 200), 200, badShip); } else { aEnemy = new Enemy(500 + (i * 200), 400, badShip); } } 

The reason for this is that you have to write less code to do the same thing, and it will stop you from making silly mistakes where you define a next loop to go from 19 ..30 instead of 20..30 as that would be adding only one more "else if" statement which checks if i is bigger than 20 like it is doing for the 10 case. And off course extending the loop counter to count to 30 instead of 20.
In the example of these simple loops it is easy to not make this mistake but when code gets more complex and the array gets bigger, that mistake is really easy to achieve sadly enough, and you will debug that and not immediately see what is going wrong, as one loop is overwriting data in the other which wouldn't happen in my case. It is also easier to see why the array is filled in wrongly in that case as you can see why the index fails the if statement you would expect it to go into.

##### Share on other sites
Consider using std::vector<> for managing dynamic numbers of objects (it is a resizeable array that is easy to use and hard to break). Consider using smart pointers rather than managing memory yourself. Also, if possible, see if you can keep a std::vector<> of values, this is the simplest solution.

Manual dynamic allocation should be rare. You probably don't need it here.

##### Share on other sites
You could have a look up table for flexibility. This would allow to you to have non-regular occurrences:

 int PhaseY[10]={200,400,500,800,1000,1100,1200,1500,1600,1650}; for (int i = 0; i < 100; i++) { aEnemy = new Enemy(500 + (i * 200), PhaseY[i/10], badShip); } 

##### Share on other sites
If I can suggest something, as rip-off said, use a std::vector... but most importantly, use a data driven approach to place your enemies. XML could be a good option (check Tiny XML, that's quite easy to use)... I know this might not be "beginner friendly", but you'll see the benefit quite soon You'll be able to use your XML for many more settings and it will be easier when you'll decide to configure your Enemies (sprite, behavior, etc...)

Hope this helps

1. 1
2. 2
3. 3
Rutin
21
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 12
• ### Forum Statistics

• Total Topics
632568
• Total Posts
3007119

×