Jump to content
  • Advertisement
Sign in to follow this  
Elit3d

[SFML] My vector array is only drawing one sprite

This topic is 889 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

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 this post


Link to post
Share on other sites
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.) Edited by Khatharr

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!