这是我用来获取包含以下变量的代码screenHeight
:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Ball pall;
MinuPad playerPad;
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;
但是我不能在不同的类中使用该变量。例如,我有一个名为的类Ball.cs
,我还需要使用关于screenWidth
和的信息screenHeight
。
当我尝试使用GraphicsDevice.Viewport.Width
它时,它给了我:
An object reference is required for the non-static field, method, or property
'Microsoft.Xna.Framework.Graphics.GraphicsDevice.Viewport.get'
我的问题:为什么我不能GraphicsDevice.Viewport
在其他课程中使用?我应该怎么做才能解决问题?