Hi All,
Running into a problem with my texture, and I believe it's because the texture is going out of scope when I try and draw the sprite in another function. How do you generally get around this, as I'm sure most would want to initialize the sprite and its properties before using it later.
Here is the "initializer":
void Unit::setImage(string unitFP){
if (!localTexture.loadFromFile(unitFP)){
return;}
localSprite.setTexture(localTexture); }
And here is the code that will determine where to draw the sprite:
void Unit::displaySprite(sf::RenderWindow& window){
localSprite.setPosition(xLoc, yLoc);
window.draw(localSprite); }
The instance is what I expect it to be, since xLoc and yLoc are set correctly, but the texture is lost and instead only a white quad is shown.
Can anyone lend a hand with their own personal ideas as to how to solve this problem? Thanks!!!
NOTE: I have declared localSprite and localTexture in the header file.
Exploiter, I had an example at the top. Here's another shot at being more thorough:
EDIT: More code...
This is from Player class. units is a vector of all units a player will have.
void Player::PlayerInit(){
Unit newUnit("Alien.png");
units.push_back(newUnit);
}
Unit::Unit(string FP){
filePath = "images/" + FP;
if (!localTexture.loadFromFile(filePath)){
return;}
}
void Unit::displaySprite(sf::RenderWindow& window){
localSprite.setPosition(xLoc, yLoc);
localSprite.setTexture(localTexture);
window.draw(localSprite);
}
Both snippets are from the class "Unit.cpp" I know that I am using the correct class instances because xLoc and yLoc are being set correctly in another function.
Now, let me be clear - the above works, but only the FIRST time I create a new "Unit" instance. When I create more units, the picture is just ... gone. I call loadFromFile on localTexture, it does not fail, which is why it seems like something is stopping me from accessing the same resource :-\
I am trying my best to follow the guidelines ... minimal and complete.