Excellent, that gives all the information needed.
In C++, there's something called a 'scope'. Variables only exist until the code's execution reaches the end of the scope that the variable was contained in.
Variables in one scope can be accessed from variables in a nested scope, but variables in a nested scope can't be accessed from the owning scope.
void myFunction()
{ //Start of scope A
int variableInScopeA;
{ //Start of scope B
int variableInScopeB;
variableInScopeA = 27; //Fine. I can access variables from parent scopes.
} //End of scope B
variableInScopeB = 27; //Not fine. 'variableInScopeB' doesn't exist in this scope.
} //End of scope A
Since functions themselves are scopes, then you can't access a variable that's declared in one function in any other function.
You can only pass the variables in through parameters - either copying the value into a local variable, or using a reference variable that pretends it's the same variable.
void functionA()
{
int myVariableInFunctionA = 357;
}
void functionB()
{
myVariableInFunctionA = 777; //Error! 'myVariableInFunctionA' doesn't exist in functionB's scope.
}
Even if you created an identically named variable in 'functionB()', it'd just be a separate variable that happens to be named the same. It wouldn't really be the same variable.
That's problem one - accessing variables in different scopes.
Problem two is, variables get destroyed when they reach the end of the scope they are in.
So if I do this:
void myFunction()
{
sf::Sprite mySprite;
{
sf::Texture myTexture;
//...
mySprite.setTexture(myTexture);
} //<--- 'myTexture' gets destroyed here when it goes out of scope, so mySprite now has an invalid texture!
} //<--- 'mySprite' gets destroyed here, because it now goes out of scope.
So there are two problems:
1) How do you make sure 'mySprite' is accessible from the scope that your code wants to access it from?
2) How do you make sure 'myTexture' stays alive as long as, or longer than, 'mySprite' stays alive for?
First, you are creating a class already, so if you make 'mySprite' and 'myTexture' member-variables of that class, then that means both 'mySprite' and 'myTexture' will stay alive as long as that specific copy (called an 'instance') of that class stays alive.
class SpriteManager
{
private:
sf::Texture myTextureA; //The texture doesn't need to be accessed publicly in your specific code, only the sprite does.
public:
sf::Sprite mySpriteA;
};
Great! Now we can access 'mySpriteA' like this:
SpriteManager mySpriteManagerInstance;
mySpriteManagerInstance.mySpriteA.setPosition(...)
But we still need to make sure that the class loads the textures properly.
Classes have something called 'constructors'. Constructors are functions that are automaticly called just once, when an instance of a class gets created. Let's make one.
[code]class SpriteManager
{
private:
sf::Texture myTextureA; //The texture doesn't need to be accessed publicly in your specific code, only the sprite does.
public:
SpriteManager(); //The function name of a class constructors are identical to the name of the class itself.
sf::Sprite mySpriteA;
};[/code]
[code]SpriteManager::SpriteManager()
{
if(!myTextureA.load(...))
{
//Report the error and quit the program, or however your program wants to handle loading failures.
}
mySpriteA.setTexture(myTextureA);
}