我在代码的这一部分出现PacmanGame.exe错误时发生'System.NullReferenceException':
if (cells[yPosition + 1, xPosition].CellType == 'o'
|| cells[yPosition + 1, xPosition].CellType == '.'
|| cells[yPosition + 1, xPosition].CellType == '!')
public partial class Form1 : Form
{
const int cellSize = 20; //cell dimension in pixels
// The two-dimensional array of Cells that will make up the maze
private Cell[,] cells;
// the width of the map in tiles (NOT pixels)
private int mapWidth;
// the height of the map in tiles (NOT pixels)
private int mapHeight;
private Image[] pacmanImage = new Image[4];
public int currentMouthPosition { get; set; }
private Image[] ghostImage = new Image[2];
private Random rnd = new Random();
private int currentEyePosition { get; set; } //= 0;
private int score = 0;
private int lives = 3;
// private PacmanMove pacman;
private int xPositiong = 520;// initial xPosition for the ghost
private int yPositiong = 200;// initial yPosition for the ghost
private int xpos = 420;// initial xPosition for the Pacman
private int ypos = 260;// initial yPosition for the Pacman
public int pacmanisfaceing = 0;
// timer1 is for pacman
public void timer1_Tick(object sender, EventArgs e)
{
int xPosition = xpos / 20;
int yPosition = ypos / 20;
currentMouthPosition += 1;
if (currentMouthPosition > 3) currentMouthPosition = 0;
if (xPosition < 0) xPosition = this.Width;
else if (xPosition > this.Width) xPosition = 0;
if (yPosition < 0) yPosition = this.Height;
else if (yPosition > this.Height) yPosition = 0;
// if statement for detecting the walls so pacman wont go through them
if (pacmanisfaceing == 0) // used to detect walls when pacman is facing down
{
if (cells[yPosition + 1, xPosition].CellType == 'o'
|| cells[yPosition + 1, xPosition].CellType == '.'
|| cells[yPosition + 1, xPosition].CellType == '!')
{
yPosition += 10;
// CheckCollision(); // checks if pacman has had a collision with a ghost
if (cells[yPosition + 1, xPosition].CellType == '.'
&& (cells[yPosition, xPosition].IsVisible == true)) //if statement used detect a pill and if one is visible
{ // it will add up the number of pills lifted
cells[yPosition, xPosition].IsVisible = false; // this is the code for adding up the score of the pills
score += 10; // adds on 10 everytime a pill is lifted
ScoreInfoEventArgs currentScore = new ScoreInfoEventArgs(score);
OnScoreChanged(currentScore);
}
// if statement for detecting the power pills and adding on extra points
if (cells[yPosition + 1, xPosition].CellType == '!' && (cells[yPosition, xPosition].IsVisible == true))
{
cells[yPosition, xPosition].IsVisible = false;
score += 50; // adds on 50 points when a power pill is lifted
ScoreInfoEventArgs currentScore = new ScoreInfoEventArgs(score);
OnScoreChanged(currentScore);
//SoundPlayer myPlayerMove = new SoundPlayer("..\\..\\Sounds\\pacman_eatfruit.wav"); // plays the eat fruit tune when a power pill is lifted
// myPlayerMove.Play();
}
}
}
答案 0 :(得分:0)
在该特定行中只有两件事可能为空
(cells[yPosition + 1, xPosition].CellType == 'o' || cells[yPosition + 1, xPosition].CellType == '.' || cells[yPosition + 1, xPosition].CellType == '!')
如果 cells 为null,那么这是一个简单的修复 - 只是意味着单元格引用的对象尚未实例化。如果数组元素为null,则单元格索引的逻辑或填充单元格数组的逻辑是错误的。