查看窗口区域?

时间:2014-12-30 01:35:05

标签: c# visual-studio-2013 null xna

您好我需要找出我游戏窗口的观看区域的大小。我找到了这段代码:

int height = GraphicsDevice.Viewport.Height;

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

你能帮我解决这个问题吗?谢谢,抱歉我的英文

2 个答案:

答案 0 :(得分:0)

高度看起来可能是一个类或其他东西,因为它的大小写,但最大的线索是错误中的部分,它表示对象引用。最后的.Height获取Viewport的高度对象。您需要访问对象的属性/成员变量(可能是命名高度;找到它的最简单方法是代码完成)以将其分配给变量。

答案 1 :(得分:0)

我会说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;
    }
}
相关问题