3
\$\begingroup\$

I work on a little project, and in the past, I used SpriteManager : DrawableGameComponent as my SpriteManager class.

I have been told that DrawableGameComponent is not so recommended to use, so I decided to make my own custom methods in a new class called SpriteManagerGeneral. Now I try to acces that class via Game. In the past I used this in Game:

spriteManager = new SpriteManager(this);
Components.Add(spriteManager);

But now I can't do this with SpriteManagerGeneral because it's not a GameComponent.

Do I just change my class (and edit my methods) to SpriteManagerGeneral : GameComponent or is there another way (that is better than GameComponent) to do this?

NOTE: I'm a beginner in programming.

NOTE2: Yes I know why DrawableGameComponent is not so recommended.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ "I have been told that DrawableGameComponent is not so recommended to use" Did you understand why, so you aren't replacing it with an even worse solution? What exactly does your SpriteManagerGeneral do and how are other parts of your application interacting with it? \$\endgroup\$
    – Philipp
    Commented Nov 13, 2015 at 19:12
  • \$\begingroup\$ All you have to do is create a class without deriving it from GameComponent, or DrawableGameComponent. Incorporate your own Draw and Update methods, and call them. And instead of using Components.Add() you would simply create a reference, in you Game class, SpriteManager. \$\endgroup\$
    – jgallant
    Commented Nov 13, 2015 at 20:41
  • \$\begingroup\$ @Philipp Yes I understand why. My SpriteManagerGeneral does exactly the same BUT it has another name because I keep the other Spritemanager class if something goes wrong. My class is used to handle all the sprites. \$\endgroup\$
    – Jelle
    Commented Nov 13, 2015 at 21:50
  • \$\begingroup\$ @Jelle So what you are actually asking is how to write a class which is a valid GameComponent? \$\endgroup\$
    – Philipp
    Commented Nov 13, 2015 at 22:43
  • \$\begingroup\$ No, my question actually was how to use my SpriteManager class (wich is not derived from anything) in Game. Sorry for the confusion. \$\endgroup\$
    – Jelle
    Commented Nov 13, 2015 at 23:41

1 Answer 1

2
\$\begingroup\$

Your SpriteManager class would not derive from anything. You would write in your own Update and Draw functions:

public class SpriteManager 
{
    GraphicsDevice GraphicsDevice;

    public SpriteManager(GraphicsDevice graphicsDevice)
    {
        GraphicsDevice = graphicsDevice;
        LoadContent();
    }

    private void LoadContent()
    {
        //Use GraphicsDevice to load content here
    }

    public void Update(GameTime gametime)
    {
         float elapsed = (float)gametime.ElapsedGameTime.TotalSeconds;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
         spriteBatch.Draw(foo);
    }
}

Don't forget to call Update() in your Update, and Draw() in your Draw from the main Game class:

...

SpriteBatch SpriteBatch;
SpriteManager SpriteManager;

protected override void LoadContent()
{
     SpriteBatch = new SpriteBatch(GraphicsDevice);
     SpriteManager = new SpriteManager(GraphicsDevice);
}

protected override void Update(GameTime gameTime)
{
    SpriteManager.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    SpriteBatch.Begin();
    SpriteManager.Draw(SpriteBatch);
    SpriteBatch.End();
}

...
\$\endgroup\$
3
  • \$\begingroup\$ What do you mean by "main gameloop". Do you mean the Game class? \$\endgroup\$
    – Jelle
    Commented Nov 13, 2015 at 21:56
  • \$\begingroup\$ Yes. The main game class. I will revise in a bit, I'm out drinking at the moment. \$\endgroup\$
    – jgallant
    Commented Nov 13, 2015 at 21:58
  • 1
    \$\begingroup\$ Ok, you are really helpfull on my questions! I will try this as soon as I can. \$\endgroup\$
    – Jelle
    Commented Nov 13, 2015 at 22:00

You must log in to answer this question.

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