我得到这个错误抱歉所有这一切我搞砸了我需要更改值和 不知道这是怎么回事如何改变null的值
字段永远不会分配给,并且始终具有默认值nulls
在以下代码中,指示:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// this is ling gives me field is never assigned to and will alwayhave defult value nulls c#
Texture2D marioTexture;
int marioYPos=100;
int marioXPos=100;
int marioWidth=64;
int marioHeight=64;
// and this line give me field is never assigned to and will alwayhave defult value nulls c#
Texture2D PongBallFinalTexture;
int PongBallFinalYpos=50;
int PongBallFinalXpos=50;
int PongBallFinalWidth=32;
int PongBallFinalHeight=32;
graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
spriteBatch.Begin ();
spriteBatch.Draw (marioTexture, new Rectangle (marioXPos, marioYPos, marioWidth, marioHeight), Color.White);
base.Draw (gameTime);
spriteBatch.Draw (PongBallFinalTexture, new Rectangle (PongBallFinalXpos, PongBallFinalYpos, PongBallFinalWidth, PongBallFinalHeight), Color.White);
base.Draw (gameTime);
spriteBatch.End ();
答案 0 :(得分:0)
好吧,在您发布的代码中,您永远不会将任何值分配给给出错误的那两个字段,您只需声明它们。在C#中不允许使用未初始化的变量。这两个特别总是具有空值,因为它是除Default Values Table (C# Reference)中描述的基本语言类型之外的对象类型变量的默认值。
要使其正常工作,您需要分配值,与在此处的位置和大小相同。例如。直接在声明中:
Texture2D marioTexture = new Texture2D(graphics.GraphicsDevice, marioWidth, marioHeight);
Texture2D PongBallFinalTexture = new Texture2D(graphics.GraphicsDevice, PongBallFinalWidth, PongBallFinalHeight);
程序中的或更高版本,与声明分开,但在使用之前:
Texture2D marioTexture;
Texture2D PongBallFinalTexture;
...
marioTexture = new Texture2D(graphics.GraphicsDevice, marioWidth, marioHeight);
PongBallFinalTexture = new Texture2D(graphics.GraphicsDevice, PongBallFinalWidth, PongBallFinalHeight);
使用basic constructor。它只是一个例子,您必须知道如何构建纹理,分配给它们的内容。