Multiple instances of one class

Started by
14 comments, last by mypel16000 11 years, 5 months ago

Ok, thank you for all these ideas, but could someone explain to me how to make a vector containing items and how to add and delete elements from it.
http://www.cplusplus.com/reference/stl/vector/push_back/
http://www.cplusplus.com/reference/stl/vector/erase/
Advertisement
Ok... that's not helping at all. Can you just show me how I would make my code. I have a person class containing all functions for a zombie, what i want to do is create a vector, make a certain amount of zombies(person class) inside them according to a rand() function, initialize them all and then during my main loop to update them. I thought of this code, but it just makes my player go really laggy and no zombies appear:

[source lang="cpp"] vector<Player> zombies (5);
Player *zombieO;

for (int n=0; n<5; n++)
{
int xR = rand() % 1000 + 1;
int yR = rand() % 800 + 1;
zombieO = new Player;
zombieO->initialize(xR, yR, 50);
zombieO->loadContent();
zombies.push_back(*zombieO);
}

while(Window.IsOpened())
{
sf::Event Event;
while(Window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
Window.Close();
}


vector<Player>::iterator it;
for ( it = zombies.begin(); it != zombies.end(); ++it ) {
zombieO->tick(player, Window);
zombieO->draw(Window);
}[/source]

Any help or other ideas? Also, can you also tell me how to do projectiles?

Ok... that's not helping at all. Can you just show me how I would make my code. I have a person class containing all functions for a zombie, what i want to do is create a vector, make a certain amount of zombies(person class) inside them according to a rand() function, initialize them all and then during my main loop to update them. I thought of this code, but it just makes my player go really laggy and no zombies appear:

Any help or other ideas? Also, can you also tell me how to do projectiles?


mypel,

I've given you a link to look directly at my source code (you can download the full source from my Old Blog), and you've been given the webpage that explains vectors. You need to do some work yourself.

Your code looks OK, but there's still some issues. You don't need to declare how big a vector is, it is dynamic, so you can just push_back zombie's as you need them. ALso, don't create a new player pointer for the zombies just push an instance of them onto th vector, like this:

for (int i = 0; i < 5; ++i) {
int xR = rand() % 1000 + 1;
int yR = rand() % 800 + 1;
// Your player class should have the cxonstructor define the x, y, and (whatever 50 is), not a separate call to initialize
// the constructor can probably do whatever loadContent does too
Player zombie(xR, yR, 50);
zombies.push_back(zombie);
}


Also, you're not looping through the zombies at all. Zombie0 is just the last zombie created in your loop. You need to be access the zombies form the vector. Again, the vector page will help, but here's what I would do:

for (int = 0; i < zombies.size(); ++i) {
zombies->tick(player, Window);
zombies->draw(Window);
}


And projectiles aren't much different than adding a zombie as far as adding them to a vector, and looping through them all to process them.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thank you, but now its got worse, my program just crashes straight away, I don't know what to do... my code is probably full of bugs, Please, if someone could have a look over it and help me, that would be great! The files are here http://uploading.com/files/get/bd1494c6/moving%2Bman.rar

Please help, I have tried vectors with other games and they work, but there's a problem with this one.
What IDE are you using? You need to learn to use the debugger, and find out what line is causing the crash? If you run it with the debugger, it will stop on the line that causes the crash.

Worse case, you throw some prints inside your code, see which line gets print and which doesn't. Then, work from there.

Honestly I think you need to step back from the game scene, and work on playing with objects in vectors, and looping through the objects and doing things to them in a simple program. Once you understand how it works, then you can move to the next thing.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I have stepped back... I've done things like having a circle move around the screen and if you press a another one appears and if you press x it dissapears. I've also done an age, name and gender database using vectors...... that's not the problem anymore! I did what you did by commenting things out, and found the source of the problem, it is in the function tick(), and that always worked when I was working with 1 zombie...

This topic is closed to new replies.

Advertisement