窗口的可视区域

本文关键字:区域 可视 窗口 | 更新日期: 2023-09-27 18:35:34

嗨,我需要找出游戏窗口的查看区域的大小。我找到了这个代码:

int height = GraphicsDevice.Viewport.Height;

但返回此错误:对象引用未设置为对象的实例。

你能帮我如何解决这个问题吗?谢谢,对不起我的英语

窗口的可视区域

我会说GraphicsDevice.Viewport.Height是空的,还没有启动。 GraphicsDevice位于命名空间Microsoft.Xna.Framework.Graphics内。

如果您从游戏类继承,请尝试:

this.GraphicsDevice.Viewport.Height

或与

this.Game.GraphicsDevice.Viewport.Height

您尝试在哪里填充这些整数?

新增示例

using Microsoft.Xna.Framework.Graphics;
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    public int screenWidth;
    public int screenHeight;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    protected override void Initialize()
    {
        screenWidth = GraphicsDevice.Viewport.Width;
        screenHeight = GraphicsDevice.Viewport.Height;
    }
}

由于其大写,高度看起来像是一个类或其他东西,但最大的线索是错误中它说对象引用的部分。这。末尾的高度获取视口的高度对象。您需要访问对象的属性/成员变量(可能名为 height;找到它的最简单方法是代码完成)以将其分配给变量。