我正在关注XNA 4.0 Game Development By Example: A Beginner's Guide 一书,但我不断收到 Null Reference Exception 错误。特别是在game.Tick
我的代码部分。只是想知道我是否编码不正确,或者 Visual Studio 是否只是表现得很奇怪。谢谢
namespace Level_Editor
{
public partial class MapEditor : Form
{
public Game1 game;
public MapEditor()
{
InitializeComponent();
}
private void LoadImageList()
{
string filepath = Application.StartupPath +
@"\Content\Textures\PlatformTiles.png";
Bitmap tileSheet = new Bitmap(filepath);
int tilecount = 0;
for (int y = 0; y < tileSheet.Height / TileMap.TileHeight; y++)
{
for (int x = 0; x < tileSheet.Width / TileMap.TileWidth; x++)
{
Bitmap newBitmap = tileSheet.Clone(new
System.Drawing.Rectangle(
x * TileMap.TileWidth,
y * TileMap.TileHeight,
TileMap.TileWidth,
TileMap.TileHeight),
System.Drawing.Imaging.PixelFormat.DontCare);
imgListTiles.Images.Add(newBitmap);
string itemName = "";
if (tilecount == 0)
{
itemName = "Empty";
}
if (tilecount == 1)
{
itemName = "White";
}
listTiles.Items.Add(new
ListViewItem(itemName, tilecount++));
}
}
FixScrollBarScales();
}
private void FixScrollBarScales()
{
Camera.ViewPortWidth = pctSurface.Width;
Camera.ViewPortHeight = pctSurface.Height;
Camera.Move(Vector2.Zero);
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum =
Camera.WorldRectangle.Height -
Camera.ViewPortHeight;
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum =
Camera.WorldRectangle.Width -
Camera.ViewPortWidth;
}
private void loadMapToolStripMenuItem_Click(
object sender,
EventArgs e)
{
try
{
TileMap.LoadMap(new FileStream(
Application.StartupPath + @"\MAP" +
cboMapNumber.Items[cboMapNumber.SelectedIndex] + ".MAP",
FileMode.Open));
}
catch
{
System.Diagnostics.Debug.Print("Unable to load map file");
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
game.Exit();
Application.Exit();
}
private void MapEditor_Load(object sender, EventArgs e)
{
LoadImageList();
cboCodeValues.Items.Clear();
cboCodeValues.Items.Add("Gemstone");
cboCodeValues.Items.Add("Enemy");
cboCodeValues.Items.Add("Lethal");
cboCodeValues.Items.Add("EnemyBlocking");
cboCodeValues.Items.Add("Start");
cboCodeValues.Items.Add("Clear");
cboCodeValues.Items.Add("Custom");
for (int x = 0; x < 100; x++)
{
cboMapNumber.Items.Add(x.ToString().PadLeft(3, '0'));
}
cboMapNumber.SelectedIndex = 0;
TileMap.EditorMode = true;
backgroundToolStripMenuItem.Checked = true;
}
private void timerGameUpdate_Tick(object sender, EventArgs e)
{
if (hScrollBar1.Maximum < 0)
{
FixScrollBarScales();
}
game.Tick();
if (game== null && game.HoverCodeValue != lblCurrentCode.Text)
lblCurrentCode.Text =game.HoverCodeValue;
}
private void MapEditor_FormClosed(
object sender,
FormClosedEventArgs e)
{
game.Exit();
Application.Exit();
}
}
}