【问题标题】:using GraphicsDeviceManager in another class在另一个类中使用 GraphicsDeviceManager
【发布时间】:2013-08-13 18:46:53
【问题描述】:

我正在尝试在不同于 Game1 类的类中使用 GraphicsDeviceManager,无论我是否初始化它,它都会给我一个错误。这是我正在做的一个示例:

    public class ClassB : ClassC
    {
       GraphicsDeviceManager graphics;

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

它告诉我它无法将此类转换为 Microsoft.Xna.Framework.Game。

【问题讨论】:

  • 什么是Game1? ClassC 是什么?您收到的确切错误消息是什么?实例化 GraphicsDeviceManager(this) 的实例应该可以正常工作。
  • ClassC 是从 XNA 的 Game 类派生的吗?
  • 是的,C类继承自Game类

标签: c# xna multiple-inheritance


【解决方案1】:

您应该始终在 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;

为了让您访问图形管理器。

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果你的类是从Game 继承的,我认为GraphicsDeviceManager 已经存在,(可能你会收到这样的错误消息),所以你不能再次声明它。
    您可以简单地将它作为新类的构造函数中的参数传递。或者您可以只传递您需要在该类中使用的成员。

    关于“它告诉我它无法将此类转换为 Microsoft.Xna.Framework.Game”,如果您的类继承自 Game,则您不能将 ClassB 作为 Game 传递,只需尝试显式转换它即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      相关资源
      最近更新 更多