user2529011
user2529011

Reputation: 743

using GraphicsDeviceManager in another class

I am trying to use GraphicsDeviceManager in a class different from Game1 class and regardless of whether or not I initialize it it gives me ana error. This is a sample of what I am doing:

    public class ClassB : ClassC
    {
       GraphicsDeviceManager graphics;

       public ClassB()
       {
           graphics = new GraphicsDeviceManager(this);//This is where I am getting an error
       }
    }

It tells me that it cannot convert this class to Microsoft.Xna.Framework.Game.

Upvotes: 1

Views: 804

Answers (2)

SD1990
SD1990

Reputation: 808

You should always have the Graphics Manager in the Game1 class in instead pass the manager from that as a parameter.

so Game1.cs

GraphicsManager graphics;

ClassB classyclass = new Class(graphics);

and the new class

public class ClassB : ClassC
    {
       public ClassB(GraphicsManager graphics)
       { 
          //Use the Graphics stuff 
       }
    }

Also i think youd benefit from using GameComponents (GameComponents) which set up most of the stuff for you and take Game1 as a parameter so you can do something like this:

((Game)Game1).graphics;

in order for you to access the graphics manager.

Hope this helps

Upvotes: 1

pinckerman
pinckerman

Reputation: 4213

If your class is inherited form Game I think GraphicsDeviceManager is already present, (probably you'll get an error message like this), so you can't declare it again.
You can simply pass it like a parameter in the constructor of the new class. Or you can pass only the member you need to use in that class.

Regarding "It tells me that it cannot convert this class to Microsoft.Xna.Framework.Game", you can't pass your ClassB as Game, if your class inherits from Game just try to cast it explicitly.

Upvotes: 1

Related Questions