[SFML] My vector array is only drawing one sprite

Started by
5 comments, last by Khatharr 8 years, 3 months ago

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);
}
Advertisement
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.)
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.


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.

Hello to all my stalkers.

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);
}

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

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);

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement