• Create Account

### #ActualBeerNutts

Posted 02 November 2012 - 08:12 AM

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[i]->tick(player, Window);
zombies[i]->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.

### #1BeerNutts

Posted 02 November 2012 - 08:11 AM

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?

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:
[code]
for (int = 0; i < zombies.size(); ++i) {
zombies[i]->tick(player, Window);
zombies[i]->draw(Window);
}


PARTNERS