Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sprites aren't properly changing their member variables  (Read 1790 times)

0 Members and 1 Guest are viewing this topic.

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
sprites aren't properly changing their member variables
« on: July 09, 2010, 03:44:21 pm »
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:
Code: [Select]


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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sprites aren't properly changing their member variables
« Reply #1 on: July 09, 2010, 03:59:54 pm »
GetSprite() is probably returning by value (ie. a new copy every time it is called), it should return a reference (sf::Sprite&).
Laurent Gomila - SFML developer

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
sprites aren't properly changing their member variables
« Reply #2 on: July 09, 2010, 04:05:07 pm »
Quote from: "Laurent"
GetSprite() is probably returning by value (ie. a new copy every time it is called), it should return a reference (sf::Sprite&).


ah ha.  Thank you. I had tried that earlier and was getting compiler errors.  Silly me forgot to delete the const.