I have several sprites that I am trying to print to the screen. These sprites are members of the class VariableLabel and are all of the same image. VariableLabel's initialize function sets each new sprite to this static image.
Several VariableLabel objects are private members of my simulation class. When I try to modify the SubRect or apply move or do something else that alters the properties of the sprites, nothing happens. They are drawn the same way, regardless of how I choose to alter the sprites. In essence, each sprite is drawn simply on top of the one before it.
I was thinking there might be scoping issues, but the problem isn't apparent to me.
Here is a little code to help:
bool Simulation::Initialize()
{
// Load images from the data files
if (!img_background.LoadFromFile("Resources/Background.png") ||
!img_variableLabel.LoadFromFile("Resources/VariableLabel.png"))
return EXIT_FAILURE;
// Set images to sprites
sprite_background.SetImage(img_background);
sprite_trajectory.SetImage(img_variableLabel);
// Set sprite properties
sprite_trajectory.SetSubRect(sf::IntRect(13, 21, 164, 60));
sprite_trajectory.SetScaleX(1.5);
sprite_trajectory.SetPosition(500.f, 10.f);
sprite_trajectory.SetScale(1.f, 1.f);
posX.GetSprite().SetSubRect(sf::IntRect(13, 21, 164, 60));
posX.GetSprite().SetPosition(250.f, 250.f);
posX.GetSprite().SetScale(1.f, 1.f);
posY.GetSprite().SetSubRect(sf::IntRect(13, 21, 164, 60));
posY.GetSprite().SetPosition(350.f, 250.f);
posY.GetSprite().SetScale(1.f, 1.f);
posZ.GetSprite().SetSubRect(sf::IntRect(13, 21, 164, 60));
posZ.GetSprite().SetPosition(300.f, 250.f);
posZ.GetSprite().SetScale(1.f, 1.f);
return true;
}
void Simulation::Render()
{
MainWindow.Clear();
MainWindow.Draw(sprite_background);
MainWindow.Draw(posX.GetSprite());
MainWindow.Draw(posY.GetSprite());
MainWindow.Draw(posZ.GetSprite());
MainWindow.Draw(sprite_trajectory);
MainWindow.Display();
}
void Simulation::HandleEvents()
{
const sf::Input& input = MainWindow.GetInput();
if(event.Type == sf::Event::Closed )
MainWindow.Close();
if((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
MainWindow.Close();
// Move the sprite
if (input.IsKeyDown(sf::Key::Left)) posZ.GetSprite().Move(-100 * .1, 0);]
}
void Simulation::Run()
{
while(MainWindow.IsOpened())
{
while(MainWindow.GetEvent(event))
{
HandleEvents();
}
Update();
Render();
}
}
Note that sprite_trajectory and sprite_background are working well
Thank you
-Nick