Jump to content
  • Advertisement
Sign in to follow this  
rherm23

Loading an Image in a class

This topic is 2643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!