2014-12-30 144 views
0

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

int height = GraphicsDevice.Viewport.Height; 

,但这个错误:未设置为一个对象的实例 对象引用。

你能帮助我解决这个问题吗?谢谢并对我的英语感到抱歉

回答

0

我会说GraphicsDevice.Viewport.Height为空然后,还没有inited。 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; 
    } 
} 
0

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