2010-04-11 149 views
0

「對象引用未設置爲對象的實例」。空引用異常

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 

namespace XNAdev 
{ 
    class Sprite 
    { 
     //The size of the Sprite 
     public Rectangle Size; 

     //Used to size the Sprite up or down from the original image 
     public float Scale = 1.0f; 

     //The current position of the Sprite 
     public Vector2 Position = new Vector2(115, 0); 
     //The texture object used when drawing the sprite 
     private Texture2D mSpriteTexture; 

     //Load the texture for the sprite using the Content Pipeline 
     public void LoadContent(ContentManager theContentManager, string theAssetName) 
     { 
      mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); 
      Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 
     } 

     //Draw the sprite to the screen 
     public void Draw(SpriteBatch theSpriteBatch) 
     { 
      theSpriteBatch.Draw(mSpriteTexture, Position, 
       new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White, 
       0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0); 

     }  
    } 
} 

我對這個C#非常新,所以任何幫助都會很棒。

我不知道我的錯誤是什麼。


namespace XNAdev 
{ 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Sprite mSprite; 
     Sprite mSpriteTwo; 
     Sprite mBackgroundOne; 
     Sprite mBackgroundTwo; 
     Sprite mBackgroundThree; 
     Sprite mBackgroundFour; 
     Sprite mBackgroundFive; 





     public Game1() 
     {   

      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     protected override void Initialize() 
     { 
      mSprite = new Sprite(); 
      mSpriteTwo = new Sprite(); 

      mBackgroundOne = new Sprite(); 
      mBackgroundOne.Scale = 2.0f; 

      mBackgroundTwo = new Sprite(); 
      mBackgroundTwo.Scale = 2.0f; 

      mBackgroundThree = new Sprite(); 
      mBackgroundThree.Scale = 2.0f; 

      mBackgroundFour = new Sprite(); 
      mBackgroundFour.Scale = 2.0f; 

      mBackgroundFive = new Sprite(); 
      mBackgroundFive.Scale = 2.0f; 

      base.Initialize(); 
     } 

     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      mSprite.Position = new Vector2(125, 245); 

      mSpriteTwo.LoadContent(this.Content, "SquareGuy"); 
      mSpriteTwo.Position.X = 300; 
      mSpriteTwo.Position.Y = 300; 

      mBackgroundOne.LoadContent(this.Content, "Background01"); 
      mBackgroundOne.Position = new Vector2(0, 0);    

      mBackgroundTwo.LoadContent(this.Content, "Background02"); 
      mBackgroundTwo.Position = new Vector2(mBackgroundOne.Position.X + mBackgroundOne.Size.Width, 0); 

      mBackgroundThree.LoadContent(this.Content, "Background03"); 
      mBackgroundThree.Position = new Vector2(mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width, 0); 

      mBackgroundFour.LoadContent(this.Content, "Background04"); 
      mBackgroundFour.Position = new Vector2(mBackgroundThree.Position.X + mBackgroundThree.Size.Width, 0); 

      mBackgroundFive.LoadContent(this.Content, "Background05"); 
      mBackgroundFive.Position = new Vector2(mBackgroundFour.Position.X + mBackgroundFour.Size.Width, 0);    
     } 

     protected override void UnloadContent() 
     { 

     } 


     protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 

      if (mBackgroundOne.Position.X < -mBackgroundOne.Size.Width) 
      { 
       mBackgroundOne.Position.X = mBackgroundFive.Position.X + mBackgroundFive.Size.Width; 
      } 

      if (mBackgroundTwo.Position.X < -mBackgroundTwo.Size.Width) 
      { 
       mBackgroundTwo.Position.X = mBackgroundOne.Position.X + mBackgroundOne.Size.Width; 
      } 

      if (mBackgroundThree.Position.X < -mBackgroundThree.Size.Width) 
      { 
       mBackgroundThree.Position.X = mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width; 
      } 

      if (mBackgroundFour.Position.X < -mBackgroundFour.Size.Width) 
      { 
       mBackgroundFour.Position.X = mBackgroundThree.Position.X + mBackgroundThree.Size.Width; 
      } 

      if (mBackgroundFive.Position.X < -mBackgroundFive.Size.Width) 
      { 
       mBackgroundFive.Position.X = mBackgroundFour.Position.X + mBackgroundFour.Size.Width; 
      } 

      Vector2 aDirection = new Vector2(-1, 0); 
      Vector2 aSpeed = new Vector2(160, 0); 

      mBackgroundOne.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; 
      mBackgroundTwo.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; 
      mBackgroundThree.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; 
      mBackgroundFour.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;    
      mBackgroundFive.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;    
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      graphics.GraphicsDevice.Clear(Color.CornflowerBlue);   

      spriteBatch.Begin(); 

      mBackgroundOne.Draw(this.spriteBatch); 
      mBackgroundTwo.Draw(this.spriteBatch); 
      mBackgroundThree.Draw(this.spriteBatch); 
      mBackgroundFour.Draw(this.spriteBatch); 
      mBackgroundFive.Draw(this.spriteBatch); 

      mSprite.Draw(this.spriteBatch); 
      mSpriteTwo.Draw(this.spriteBatch); 
      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

那代碼

+4

它發生在哪裏? – Femaref 2010-04-11 16:36:24

+0

我們不知道錯誤在哪裏。 – 2010-04-11 16:41:44

+0

當您使用未初始化的變量時,會發生空引用。所以這個錯誤必須在loadcontent或draw方法中。 – 2010-04-11 16:42:50

回答

1

我已經看了一遍,並設法使用驗證,如果你正在繪製的精靈有一個空引用(沒有紋理),它會忽略它,並繼續繪製其他所有。

變化在Sprite.cs您的draw()方法,以這樣的:

//Draw the sprite to the screen 
public void Draw(SpriteBatch theSpriteBatch) 
{ 
    if (mSpriteTexture != null) 
    { 
     theSpriteBatch.Draw(mSpriteTexture, Position, 
      new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White, 
      0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0); 
    } 
} 

出現該問題,因爲你永遠不放棄 「雪碧mSprite;」 一質地,只有一個位置。

快速片段:

protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    mSprite.Position = new Vector2(125, 245); 

    mSpriteTwo.LoadContent(this.Content, "SquareGuy"); 
    mSpriteTwo.Position.X = 300; 
    mSpriteTwo.Position.Y = 300; 

正如你可以看到你只能給mSprite的125245的位置,只要將它的紋理就像你與精靈的休息,它會正常工作。

雖然在指定紋理後您不需要從Draw()方法中刪除if(mSpriteTexture != null),但如果不是這樣,它僅僅意味着如果某些東西未被正確指定,您將不會注意到,可能是如果你以後要調試其他東西,那會很痛苦。

+0

我發佈的第一個是錯誤的地方,第二個是該類使用的代碼。 – Alex 2010-04-11 16:55:53

+0

我現在重新安裝XNA 3.1,我會給它一個 – 2010-04-11 17:38:09

+0

使用XNA 3.0的進出口 – Alex 2010-04-11 17:39:07

0

其餘也許你應該檢查

mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); 
if (mSpriteTexture != null) 
    Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); 

而且在Draw函數了。

7
  1. 查找如何設置Visual Studio以打破異常。
  2. 瞭解如何使用調試器,以便您能夠瀏覽代碼以查看錯誤發生的位置。
0

此錯誤意味着一個對象的某個字段或方法已被嘗試訪問,而未實例化該對象。

對於你的代碼似乎這是與對象mSpriteTexture發生。

您應該添加

mSpriteTexture = new Texture2D(); 

的地方,但我不能在那裏只能用這段代碼告訴。