0
\$\begingroup\$

I have a class (SpriteManager) that is not derived from Game || DrawableGameComponent || GameComponent so I use my custom methods.

When my game starts, it runs that class. My question is, how to use Game.Content.Load in a class that is not derived from any of the classes above.

I tried declaring a variable in my SpriteManager of type Game but when I use it, it returns null or an InvalidOperationException.

My variable Game:

Game game

This is my LoadContent() method:

spriteBatch = new SpriteBatch(GraphicsDevice);

//my player sprite
player = new PlayerMovement(game.Content.Load<Texture2D>(@"Images/collisionPlayer"), game.Content.Load<Texture2D>(@"Images/" + body[0]), new Vector2(150, 150), new Point(20, 35), 0, new Point(0, 0), new Point(0, 0), new Point(1, 11), 2f, 2f, 65f);

This is my SpriteManager constructor:

public SpriteManagerGeneral(GraphicsDevice graphicsDevice)
{
    GraphicsDevice = graphicsDevice;
    LoadContent();

}

And this code is in my Game class:

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(this.GraphicsDevice);
    spriteManager = new SpriteManager(GraphicsDevice);
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

If you plan to have a separate content manager in your spritemanager class, you can create one.

ContentManager myManager = new ContentManager(Game.Services, Content.RootDirectory);

This way you can handle the load and unload of sprites in your own manager. Note how the 2nd parameter is the path to the content directory, so you can have your own dir if you want.

You can pass the content manager in your constructor:

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(this.GraphicsDevice);
    spriteManager = new SpriteManager(GraphicsDevice, Content);
}
\$\endgroup\$
3
  • \$\begingroup\$ I have one question though. I can't use a ContentManager. It says that this type could not be found... Do you know what I could be doing wrong? \$\endgroup\$
    – Jelle
    Nov 15, 2015 at 0:10
  • \$\begingroup\$ Did you reference Microsoft.Xna.Framework.Content in your class? \$\endgroup\$
    – Felsir
    Nov 15, 2015 at 7:30
  • \$\begingroup\$ Thank you! It works now. I tried this already in the past but I typed Microsoft.Xna.Content instead of Microsoft.Xna.Framework.Content ... \$\endgroup\$
    – Jelle
    Nov 15, 2015 at 10:01
1
\$\begingroup\$

You have to pass the Game instance to the SpriteManager. For example:

class MyGame : Game {
 void LoadContent(ContentManager content){
   ...
   this.spriteManager = new SpriteManager(this);
 }

void Update(...)
void Draw(...)
}
\$\endgroup\$
3
  • \$\begingroup\$ Wich type is Content because I can't seem to use ContentManager in my SpriteManager constructor. I will update my question so you will see what I already have in the Game class. \$\endgroup\$
    – Jelle
    Nov 14, 2015 at 15:28
  • \$\begingroup\$ do you have your class in the same project as the game? have you imported the namespace "Microsoft.Xna.Framework.Content" ? there is the ContentManager class. \$\endgroup\$
    – EnemyArea
    Nov 15, 2015 at 0:33
  • \$\begingroup\$ Yes I forgot the .Framework in Microsoft.Xna.Framework.Content... \$\endgroup\$
    – Jelle
    Nov 15, 2015 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .