我的班级中的 GraphicsDeviceManager 对象(帖子中有更好的解释)

发布于 2024-11-30 15:25:26 字数 454 浏览 0 评论 0原文

好吧,所以我有一个 GameObject 类(我自己制作),我想创建一个包含屏幕最大宽度的 GraphicsWidth 变量,这意味着我需要创建一个 GraphicsDeviceManager 实例。 (正确的?)。好吧,这就是我所做的:

protected GraphicsDeviceManager GM;

public int GraphicsWidth
    {
        get
        {
            return GM.GraphicsDevice.Viewport.TitleSafeArea.Width;
        }
    }

它说我需要使用“new”(初始化对象)。但我不知道如何从我的 GameObject 类中做到这一点?

拜托拜托拜托,有人可以帮我吗?真的很感激。 谢谢。

编辑:有人可以吗?

Alright, so I have a GameObject class (I made by myself) and I wanted to create a for example GraphicsWidth variable that contains the Maximum width of the screen, it means I need to create a GraphicsDeviceManager instance. (Right?). Alright, this is what I did:

protected GraphicsDeviceManager GM;

public int GraphicsWidth
    {
        get
        {
            return GM.GraphicsDevice.Viewport.TitleSafeArea.Width;
        }
    }

And it says that I need to use the "new"(initialize the object). But I don't know how can I do that from my GameObject class?

Please please please, can anyone help me with this? Really appriciated.
Thanks.

EDIT: Anyone please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2024-12-07 15:25:26

GraphicsDeviceManager 在您的 Game1 类中实例化。最有可能的是,您也在 Game1 类中实例化了 gameObject。当您实例化您的游戏对象时,传递一个 GraphicDeviceManager 的引用:

//somewhere in your game1 class:
GameObject gameObject = new GameObjec(Graphics);


//then, in and above the constructor of your GameObject:
GraphicsDeviveManager GM;
public GameObject(GraphicsDeviceManager gdm)
{
   GM = gdm;//GM is now available to the rest of the gameObj class.
}

或者... 与上面相同,但传递服务,以便您可以使用它包含的任何内容。
或者...以相同的方式传递游戏引用。我看到很多人都这样做。
或者...将graphicsDeviceManager设为静态,因为只有一个。

我通常传递服务容器引用,但大多数人传递游戏引用。有些人只将引用传递给 GameObject 类中实际需要它的方法。

The GraphicsDeviceManager is instantiated in your Game1 class. Most likely, you are also instantiating your gameObject in your Game1 class too. When you instantiate your gameObject, pass a reference of the graphicDeviceManager with it:

//somewhere in your game1 class:
GameObject gameObject = new GameObjec(Graphics);


//then, in and above the constructor of your GameObject:
GraphicsDeviveManager GM;
public GameObject(GraphicsDeviceManager gdm)
{
   GM = gdm;//GM is now available to the rest of the gameObj class.
}

Or... Do the same as above but pass along the Services so you can use anything it contains.
Or... pass the Game reference along the same way. I see lots of people do that.
Or... Make the graphicsDeviceManager static since there will only be one.

I usually pass the service container ref but most people pass the game ref. Some people pass the reference only to the method in the GameObject class that actually needs it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文