• Advertisement
Sign in to follow this  

Multiple enemy objects

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I appreciate that this is, I suspect, a bit of a begginers question!

I have been playing around with C++ and DirectX9 now and have come up with a full screen and a space ship sprite which I can move about the screen.

I am now trying to create multiple enemies on the screen which will all move around independently and, of course, be their own entity. The number of enemies also has to be relevant to the level that the player is on. The code below is very basic but kind of illustrates what I am trying to do. Can I create a dynamic array of objects or is there another method of completing this task?


void makeEnemies()
{
int numOfEnemies = level * 5;
for(int x = 0; x < numOfEnemies; x++)
{
// Dynamically create an array of objects
}
}


Many thanks

Share this post


Link to post
Share on other sites
Advertisement
Sure, just create an array (clearing it each time makeEnemies() is called) and add your enemy objects to it, then you can loop through the array and do whatever you need to.

Share this post


Link to post
Share on other sites
Yes i think for testing purposes you can just define a fixed size array of your entity object. Then just define the parameters for each of them. Once you manage to get them working (you can see them moving around the screen or at least getting rendered), you can start replacing the array into a dynamic container. Assuming you can use C++ STL templates, you can use std::vector or std::list.

Share this post


Link to post
Share on other sites
You mean something like this?


Enemy* Enemies;

void makeEnemies()
{
int numOfEnemies = level * 5;

if( Enemies )
delete[] Enemies;


Enemies = new Enemy[numOfEnemies];
}

Share this post


Link to post
Share on other sites
Since you said C++ and not "plain C", use std::vector and just forget about manually fiddling with dynamic arrays. It will do all the resizing and house keeping for you and the less unnecessary code one writes himself the less potential for unnecessary bugs.

Share this post


Link to post
Share on other sites
That should work, but you still need to populate that Enemy array with actual enemies :P

Share this post


Link to post
Share on other sites

std::vector<Enemy> Enemies;

void makeEnemies()
{
int numOfEnemies =level*5;

for(int i=0;i<numOfEnemies;++i)
{
Enemies.pushback(Enemy());
}


for std::vector<Enemy>::iterator IT = Enemies.begin(); IT != Enemies.end)
{
(*IT).DoSomething(); //so something with each enemy, such as update, initialization ...
}

Enemies.clear(); //delete all enemies
}




you may also use std::vector<Enemy *> as a container for your enemies, but then you'll have to handle the allocating and deallocating the object yourself.

Best regards! Edited by kauna

Share this post


Link to post
Share on other sites
Hi Guys,

Many thanks for getting back to me. Forgive me for the delay, I have been away from the cyber world for a little while! Thanks for the advice, this has definitely answered my question. I will look in to the use os std::vector biggrin.png

Thanks again

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement