我有三个类,主要叫Game1.cs
,另外两个类在名为Images.cs
和Resize.cs
的文件夹中。在Images
中,图像被加载并且能够被使用。在Resize
中我想绘制一个名为size
的整数,它等于1.我得到一个空值错误,不知道为什么。
如果我尝试将大小放在Game1.cs
中,则会出现相同的错误。
Game1.cs
:
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;
namespace **
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public static SpriteFont font1;
private StartUp.Resize resize;
static new Rectangle con = new Rectangle(330, 246, 364, 84);
static new Rectangle cre = new Rectangle(330, 430, 364, 84);
static new Rectangle exit = new Rectangle(330, 522, 364, 84);
static new Rectangle logo = new Rectangle(216, 37, 591, 71);
static new Rectangle opt = new Rectangle(330, 338, 364, 84);
static new Rectangle play = new Rectangle(330, 154, 364, 84);
static new Rectangle ver = new Rectangle(7, 701, 215, 39);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
StartUp.Images.Content = Content;
}
protected override void Initialize()
{
this.IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
StartUp.Images.Load();
font1 = Content.Load<SpriteFont>(@"Fonts\Font1");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
resize.size = 1;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
spriteBatch.Draw(StartUp.Images.Continue, con, Color.White);
spriteBatch.Draw(StartUp.Images.Credits, cre, Color.White);
spriteBatch.Draw(StartUp.Images.Exit, exit, Color.White);
spriteBatch.Draw(StartUp.Images.Play, play, Color.White);
spriteBatch.Draw(StartUp.Images.Options, opt, Color.White);
spriteBatch.Draw(StartUp.Images.Logo, logo, Color.White);
spriteBatch.Draw(StartUp.Images.Version, ver, Color.White);
//error here
spriteBatch.DrawString(font1, resize.size.ToString(), new Vector2(0, 0), Color.White);
//error here
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Resize.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace **.StartUp
{
public class Resize
{
public int size = 1;
public int continueWidth;
public int continueHeight;
public int ContinueWidth;
public int ContinueHeight;
}
}
答案 0 :(得分:0)
那是因为你在使用之前没有初始化它。
在你的Game1构造函数中初始化它:
public Game1()
{
...
resize = new Resize();
}