嗨,我需要找出我的游戏窗口的观看面积的大小。我找到了这个密码:
int height = GraphicsDevice.Viewport.Height;但返回此错误:对象引用未设置为对象的实例。
你能帮我解决这个问题吗?谢谢,为我的英语感到抱歉
发布于 2015-01-13 09:12:50
我想说的是,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;
}
}发布于 2014-12-30 03:16:53
高度看起来可能是一个类或什么,因为它的大写,但最大的线索是在错误的部分,它说的对象引用。最后的.Height获取视图端口的高度对象。您需要访问对象的属性/成员变量(可能名为高度;找到它的最简单的方法是代码完成)来将其赋值给一个变量。
https://stackoverflow.com/questions/27698512
复制相似问题