# [SFML] My vector array is only drawing one sprite

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

## Recommended Posts

So I've got somewhat of a factory in my game that handles the Enemies. Now for some reason, it is only drawing one sprite on my screen and setting its position randomly rather than spawning in a new enemy each time. How would I actually get multiple spawning rather than just one?

Here is my code:

Game.h

	std::vector<BaseEnemy*> enemyVector;
std::vector<BaseEnemy*>::iterator enemy_iter;
Troll *troll;


Game.cpp

troll = new Troll;

troll->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);
enemyVector.push_back(troll);

for (enemy_iter = enemyVector.begin(); enemy_iter != enemyVector.end(); enemy_iter++)
{
(*enemy_iter)->draw(window);
}


Troll.cpp

void Troll::setPosition(int x, int y)
{
BaseEnemy::setPosition(x, y);
}

void Troll::draw(sf::RenderWindow &window)
{
BaseEnemy::draw(window);
}


BaseEnemy.cpp

BaseEnemy::BaseEnemy()
{
rect.setSize(sf::Vector2f(20, 20));
rect.setPosition(0, 0);
rect.setFillColor(sf::Color::Black);
}

BaseEnemy::~BaseEnemy()
{
}

void BaseEnemy::setPosition(int x, int y)
{
rect.setPosition(x, y);
}

void BaseEnemy::draw(sf::RenderWindow &window)
{
window.draw(rect);
}

Edited by Elit3d

##### Share on other sites
If you only put one thing in the container then there will only be one thing in the container. (Show the full code body, not just the parts you like.) Edited by Khatharr

##### Share on other sites

If you only put one thing in the container then there will only be one thing in the container. (Show the full code body, not just the parts you like.)

Well this is the only relevant code for now until I can get it fully working, essentially I want more than 1 troll spawning.

##### Share on other sites

Well this is the only relevant code for now

What I believe Khatarr is saying, is that in the code you consider to be relevant, we can only see you creating a single troll instance.

##### Share on other sites

Turn this part:

troll = new Troll;

troll->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);
enemyVector.push_back(troll);


Into this:

for (int i = 0; i < 10; ++i)
{
troll = new Troll;
troll->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);
enemyVector.push_back(troll);
}


##### Share on other sites

Turn this part:

troll = new Troll;

troll->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);
enemyVector.push_back(troll);


Into this:

for (int i = 0; i < 10; ++i)
{
troll = new Troll;
troll->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);
enemyVector.push_back(troll);
}


ah yes of course! thank you very much for pointing out my error :) works as intended now

##### Share on other sites

Rather than using the temporary and pushing it, you can do:

    enemyVector.emplace_back(new Troll);
enemyVector.back()->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);