完全相同的代码行使用下面几行,它工作得很好。我的老师无法理解。我在Visual Basic C#中使用XNA。当我运行我的游戏错误消息“NullReferenceException未处理”,“对象引用未设置为对象的实例。”显示。
表示错误的行:
Size = new Rectangle(0, 0, (int)(mBallTexture.Width * Scale),(int)(mBallTexture.Height * Scale));
我的代码:
class RandomBall
{
//The asset name for the Sprite's Texture
public string AssetName;
//The size of the Sprite
public Rectangle Size;
//Used to size the Sprite up or down from the original image
public float mScale = 1.0f;
//The current position of the Sprite
public Vector2 Position = new Vector2(500, 0);
//The texture object used when drawing the sprite
private Texture2D mBallTexture ;
//When the scale is modified throught he property, the Size of the
//sprite is recalculated with the new scale applied.
public float Scale
{
get {return mScale;}
set
{
mScale = value;
//Recalculate the Size of the Sprite with the new scale
//THIS CODE LINE IS THE ONE CAUSING TROUBLE
Size = new Rectangle(0, 0, (int)(mBallTexture.Width * Scale), (int)(mBallTexture.Height * Scale));
////Size = new Rectangle(0, 0, 50, 50);
}
}
//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mBallTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Size = new Rectangle(0, 0, (int)(mBallTexture.Width * Scale), (int)(mBallTexture.Height * Scale));
}