Sign in to follow this  

Multiple enemy objects

This topic is 2047 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?

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

Many thanks

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
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
[CODE]
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
}

[/CODE]


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 [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Thanks again

Share this post


Link to post
Share on other sites

This topic is 2047 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this