C# 在另一个类中使用GraphicsDeviceManager
我试图在不同于Game1类的类中使用GraphicsDeviceManager,无论我是否初始化它,它都会给我一个错误。这是我正在做的一个示例:C# 在另一个类中使用GraphicsDeviceManager,c#,xna,multiple-inheritance,C#,Xna,Multiple Inheritance,我试图在不同于Game1类的类中使用GraphicsDeviceManager,无论我是否初始化它,它都会给我一个错误。这是我正在做的一个示例: public class ClassB : ClassC { GraphicsDeviceManager graphics; public ClassB() { graphics = new GraphicsDeviceManager(this);//This is wh
public class ClassB : ClassC
{
GraphicsDeviceManager graphics;
public ClassB()
{
graphics = new GraphicsDeviceManager(this);//This is where I am getting an error
}
}
它告诉我它无法将此类转换为Microsoft.Xna.Framework.Game 如果您的类是从
游戏
继承的,我认为GraphicsDeviceManager
已经存在(可能会收到这样的错误消息),因此您不能再次声明它。您只需像传递新类构造函数中的参数一样传递它。或者,您只能传递需要在该类中使用的成员
关于“它告诉我它不能将该类转换为
Microsoft.Xna.Framework.Game
”,您不能将ClassB作为Game
传递,如果您的类继承自游戏
,请尝试显式强制转换它。您应该始终将Game1类中的图形管理器作为参数传递给它
所以Game1.cs
GraphicsManager graphics;
ClassB classyclass = new Class(graphics);
新班级呢
public class ClassB : ClassC
{
public ClassB(GraphicsManager graphics)
{
//Use the Graphics stuff
}
}
此外,我认为使用GameComponents()会让您受益匪浅,GameComponents()为您设置了大部分内容,并将Game1作为参数,因此您可以执行以下操作:
((Game)Game1).graphics;
以便您访问图形管理器
希望这有帮助什么是
Game1
?什么是ClassC
?您收到的确切错误消息是什么?实例化GraphicsDeviceManager(this)
的实例应该可以正常工作。ClassC
是从XNA的Game
类派生的吗?是的,类C是从Game类继承的