Hi,
I'm trying to draw all the members in a list<list<Brick> > where a Brick is one of my classes. The problem is that if I use a list, all of the bricks have the color of the first brick in the first list of bricks. If I use a vector, all of them have the color of the last brick. This happens even if I just use a list<Brick> and push_back all of the bricks. I've pasted the relevant code below.
Thanks for your time and any help you can provide! Please let me know if there is anything else I should add.
// the code where I initialize the lists of bricks
list<Brick> blueBricks (window.getSize().x / 96 - 1, Brick("blueBrick.png"));
list<Brick> goldBricks (window.getSize().x / 96 - 1, Brick("goldBrick.png"));
list<Brick> greenBricks (window.getSize().x / 96 - 1, Brick("greenBrick.png"));
list<list<Brick> > bricks;
bricks.push_back(blueBricks);
bricks.push_back(goldBricks);
bricks.push_back(greenBricks);
// the brick class has private data members brickTexture and brickSprite
// this is the constructor
Brick::Brick(const string& file)
{
brickTexture.loadFromFile(file);
brickSprite.setTexture(brickTexture);
brickSprite.setOrigin(brickSprite.getLocalBounds().width / 2,
brickSprite.getLocalBounds().height / 2);
inPlay = true;
}
// this is how I draw the bricks on the window
for (list<list<Brick> >::iterator i = bricks.begin(); i != bricks.end(); ++i)
for (list<Brick>::iterator j = i->begin(); j != i->end(); ++j)
j->draw(window);
void Brick::draw(sf::RenderWindow& window)
{
if (inPlay)
{
brickSprite.setPosition(position);
window.draw(brickSprite);
}
}
I'm using SFML 2.1 with VS 2012 on Windows 7.