This is probably an easy question to answer but im new to XNA and this is slowing me down.
I want to load an image from a class i created. For instance
class Ball
{
public Texture2D image;
public int stuff;
public Ball ()
{
}
public void loadStuffs ()
{
// i want to be able to load an image here without having to pass it via parameter
image = Content.Load<Texture2D>("ball"); // this is whats not working
}
}
Anyone able to help me out here so i can load textures without having to pass them to my classes.
"choices always were a problem for you......" Maynard James Keenan
This is probably an easy question to answer but im new to XNA and this is slowing me down.
I want to load an image from a class i created. For instance
class Ball
{
public Texture2D image;
public int stuff;
public Ball ()
{
}
public void loadStuffs ()
{
// i want to be able to load an image here without having to pass it via parameter
image = Content.Load<Texture2D>("ball"); // this is whats not working
}
}
Anyone able to help me out here so i can load textures without having to pass them to my classes.
What's happening when you run that code? Are you getting a compiler error or is it just not working like you expect it to?
It's because Content is not a menber of ball, nor is it a global variable. It belongs to the Game class (which your main game derives from) so you should send it into your loadstuffs method to access it.
My XNA is a bit rusty, but it should look like this.
class Ball
{
public Texture2D image;
public int stuff;
public Ball ()
{
}
public void loadStuffs (ContentManager contentManager)
{
// i want to be able to load an image here without having to pass it via parameter
image = contentManager.Load<Texture2D>("ball"); // this is whats not working
}
}
// Then inside the MyGame class.
void FunctionName( )
{
ball.loadStuffs( Content );
}
Edit: Apparently the code tags don't like being copy & pasted from. Yay whitespace issues.