Xna NullReferenceException未处理(对象引用未设置为实例错误)



我目前正在XNA上进行一个项目。我的问题是,我试图将sprite字体加载到屏幕上(通过将其加载到Game1类中),然后继续将其传递到draw方法中的另一个类,以进一步将其绘制到屏幕上,除非我得到了继续的错误。

我试着在整个代码中查找,并意识到sprite字体在加载时确实已初始化。我以前也使用if语句来检查对象是否为null,据我所知,它不是,那么问题出在哪里呢?这是我上课的一些小消息。任何帮助都将不胜感激。也很抱歉代码太长了,我觉得需要大量的代码才能真正理解问题。

Game1类的Tid位

protected override void LoadContent()
{
#region loadTextures
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
backGround = Content.Load<Texture2D>("MainMenu");
graphics.PreferredBackBufferWidth = screenWidth; // width of screen..
graphics.PreferredBackBufferHeight = screenHeight; //height of screen..
IsMouseVisible = true;
graphics.ApplyChanges(); // load images...
HealthBar = Content.Load<Texture2D>("HealthBar");
btnPlay = new cButton(Content.Load<Texture2D>("Button1"), graphics.GraphicsDevice);
movement = new Movement(Content.Load<Texture2D>("SpritesRe"), new Vector2(rndXpos, rndYpos), 64, 57, HealthBar, cBar,Content.Load<Texture2D>("Grave"));
npc_creation = new NPC_Creation(Content.Load<Texture2D>("MonsterSprite"), screenRectangle,HealthBar,Content.Load<Texture2D>("Grave"));
btnPlay.setPosition(new Vector2(350, 300));
spritefont = Content.Load<SpriteFont>("OnscreenLevel");
StartGame();
#endregion
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
switch (CurrentGameState)
{
case GameState.MainMenu: // if mouse is clicked... call method
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
break;
case GameState.Playing: // if true.. load new image...
backGround = Content.Load<Texture2D>("Game Map");
break;
}

//checks if character takes damage from being in range of enemy
npc_creation.charWithinDamageRange(movement.getCharLocation());
//checks if character is within sight range
npc_creation.charWithinRange(movement.getCharLocation());
//calls on monster update method, with the inputs:
npc_creation.Update(gameTime, npc_creation.withinRangeOrNot(), movement.getCharLocation());
//checks if monster is in range of character attack
npc_creation.monsterTakeDamageRange(movement.getAttackRadius());
npc_creation.charWithinDamageRange(movement.getCharLocation());
if (npc_creation.withinDamageRangeOrNot() == true)
{
movement.decreaseCharHealth();
npc_creation.charWithinDamageRange(movement.getCharLocation());
npc_creation.withinDamageRangeOrNot();
}
while (npc_creation.inAttackRadius() == true)
{
npc_creation.decreaseMobHealth();
movement.resetAttackRadius();
npc_creation.monsterTakeDamageRange(movement.getAttackRadius());
npc_creation.inAttackRadius();
}
movement.Update(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
//Draw Any Scene Stuff Here
spriteBatch.Draw(backGround, Vector2.Zero, Color.White);
switch (CurrentGameState)
{
case GameState.MainMenu: // if on main menu... draw button...
btnPlay.Draw(spriteBatch);
// chardev.Draw(spriteBatch,spritefont);
break;
case GameState.Playing: // otherwise.. draw the character sprites
movement.Draw(spriteBatch);
npc_creation.Draw(spriteBatch, Color.White);
chardev.Draw(spriteBatch, spritefont);//THIS IS WHERE THE SPRITEFONT IS PASSED TO THE DRAW METHOD!!!
break;  
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}

字符开发类(绘图方法所在)

class charDevelopment
{
#region initalize

int EXP = 50; // starting exp.. must be level 1...
int LEVEL = 1;
int SP = 10; // starting skill points
int y;
#endregion 
public charDevelopment(int EXP)
{ // set value
this.EXP = EXP;
calcEXP(EXP);
}
public void calcEXP(int EXP)
{ // formula to calculate exp... arithmetic sequence... (150,300,600,1200,2400 etc..)
y = 25 * EXP * (1 + EXP);
checkLevel(y);
}
public int getEXP
{
get {return getEXP;}
set {getEXP = EXP;}
}
#region levelCheck
public void checkLevel(int y) 
{
// increment hp each level up (Can't spend skill points on it)
if (y < 50)
{
LEVEL = 1;
SP = 10;
} // NEED A MORE EFFICIENT WAY TO DO THIS... possible for loop? ideas
else if (y >= 50 && y < 150)
{
LEVEL = 1; SP += 10;
}
else if (y >= 150 && y < 300)
{
LEVEL = 2; SP += 10;
}
else if (y >= 300 && y < 600)
{
LEVEL = 3; SP += 10;
}
else if (y >= 600 && y < 1200)
{
LEVEL = 4; SP += 15;
}
else if (y >= 1200 && y < 2400)
{
LEVEL = 5; SP += 15;
}
else if (y >= 2400 && y < 4800)
{
LEVEL = 6; SP += 15;
}
else if (EXP >= 4800 && EXP < 9600)
{
LEVEL = 7; SP += 20;
}
else if (y >= 9600 && y < 19200)
{
LEVEL = 8; SP += 20;
}
else if (y >= 19200 && y < 38400)
{
LEVEL = 9; SP += 20;
}
else if (y >= 38400 && y < 76800)
{
LEVEL = 10; SP += 20;
}
}
#endregion 
public void checkSP ()
{
if(SP >0)
{
// display screen to chose skills which should be enhanced...
}
}
public void Draw(SpriteBatch spritebatch, SpriteFont spritefont)
{
spritebatch.DrawString(spritefont, "Level: " , new Vector2(5, 0), Color.White);
}
}

还有另一个类中的draw方法。。它检查敌人的怪物是否已经死亡。。。用户获得exp…将其传递到类

public void Draw(SpriteBatch spritebatch, Color color)
{ // draw the texture
if (Alive == true)
{
spritebatch.Draw(texture, pos, color);
spritebatch.Draw(HealthBar, new Rectangle((int)pos.X - 2, (int)pos.Y - 8, (int)(HealthBar.Width * ((double)CurrentHealth / 100)), 5), Color.Red);
}
else if (Alive == false) // if the monster is dead... i.e health is 0....
{// draw the grave... need a timer here...
spritebatch.Draw(Grave, pos, color);
speedXnpc = 0;
speedYnpc = 0;
monsterSpeed = 0;
tempEXP += 25;
chardev = new charDevelopment(tempEXP);// this line PASSES THE EXP
}
}

你能在线上设置一个断点吗

"chardev.Draw(spriteBatch,spritefont);">

并验证spriteFont是否为"null"?

如果您不知道断点是什么:右键单击代码行,然后单击"断点>插入断点"。如果你开始游戏,它会停在这一行,并在一个名为"Locals"的窗口中显示变量。单击窗口中"this"附近的加号,然后检查"spriteFont"-值。我希望它是清楚的。

最新更新