我不明白为什么我收到此错误消息,当我已经为p1明确分配了值时,您可以在以下代码中看到:
public class AppMain
{
private static GraphicsContext graphics;
private static bool running;
private static Sprite bckgnd1, bckgnd2;
private static Sprite openScreen, instructScreen, gameOverScreen;
private static Vector3 pos1, pos2, pos3, pos4, pos5;
private static Player p1, p2;
private static Fruit apple, orange, banana;
private static Stopwatch clock;
private static long startTime, stopTime, deltaT;
private static Random rand;
enum GameState {
opening,
instructions,
play,
gameOver};
private static GameState currentState;
private static List<Player> pList;
public static void Main (string[] args)
{
Initialize ();
while (running == true) {
startTime = clock.ElapsedMilliseconds;
SystemEvents.CheckEvents ();
Update ();
Render ();
stopTime = clock.ElapsedMilliseconds;
deltaT = stopTime - startTime;
}
}
public static void Initialize ()
{
running = true;
pList = new List<Player>();
graphics = new GraphicsContext ();
Texture2D pTex1 = new Texture2D("/Application/Assets/macmuffin.png", false);
Texture2D pTex2 = new Texture2D("/Application/Assets/macmuffin1.png", false);
pos1 = new Vector3(200, 600, 0);
pos2 = new Vector3(400, 600, 0);
Player p1 = new Player(pTex1, pos1, 1, graphics, "Player 1");
Player p2 = new Player(pTex2, pos2, 2, graphics, "Player 2");
pList.Add (p1);
pList.Add (p2);
}
此外,奖励积分如果您可以告诉我为什么当我尝试渲染玩家时,错误消息“Unhandled Exception:System.NullReferenceException:对象引用未设置为Cain_Game3.AppMain.Main上的对象实例(系统.String [] args)[0x00000] in:0“显示。感谢。
答案 0 :(得分:3)
您正在声明一个新的本地范围的p1,而不是分配到顶部附近的p1成员声明。
您可能想要更改此内容:
Player p1 = new Player(pTex1, pos1, 1, graphics, "Player 1");
到此:
p1 = new Player(pTex1, pos1, 1, graphics, "Player 1");