Loading an Image in a class

Started by
5 comments, last by Koneke 12 years, 7 months ago
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
Advertisement
Define "not working". Compiler error? Run-time error?

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?


when i type out the code i get ....

intellisense or whatever its called doesnt let me add Content to the following

image =

its like it doesnt exist. if it does come up i cant find the .Load and i get the red squigglies under the line

image = Content.Load<Texture2D>("ball");

ill try and post a screenie if need be.
I can call it from the load content function in the game1 file but not from inside a class i make.
"choices always were a problem for you......" Maynard James Keenan
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.
ill give that a shot and thanks for the replies. Much appreciated.
"choices always were a problem for you......" Maynard James Keenan
What I usually do, is that in my Game1, or whatever you call yours, I add

public static Game1 current;


Then I can load stuff by using

Game1.current.Content.Load<Texture2D>("ball");


Might be a silly way of doing it, but it's worked fine for me so far.

This topic is closed to new replies.

Advertisement