Я пытаюсь вставить звук всякий раз, когда в моей игре есть совпадение. Тем не менее, я получаю исключение NullReferenceException, когда получаю соответствие. Он переходит в строку soundEffectInstance.Play() ;. Любая помощь приветствуется. Я пытался все, пытаясь заставить его работать. Вот мой код:Null Reference Исключение при попытке воспроизведения звука
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
namespace AndyKlax.KlaxGame
{
internal class Grid
{
private const int GridOrigin = 660;
SoundEffect soundEffect;
SoundEffectInstance soundEffectInstance;
private readonly List<Block>[] _grid = new List<Block>[Klax.NumberOfColumns];
public void LoadContent(ContentManager content)
{
soundEffect = content.Load<SoundEffect>(@"Sounds/24372^pop2.mp3");
soundEffectInstance = soundEffect.CreateInstance();
}
internal Grid()
{
for (int i = 0; i < Klax.NumberOfColumns; i++)
{
_grid[i] = new List<Block>();
}
}
private bool RemoveBlocks()
{
var foundBlocksToRemove = false;
//Mark all blocks in the grid as unmatched
for (int column = 0; column < Klax.NumberOfColumns; column++)
{
for (int row =_grid[column].Count -1 ; row >=0 ; row--) //Loop backwards do its safe to remove
{
if (_grid[column][row].Delete)
{
_grid[column].RemoveAt(row);
foundBlocksToRemove = true;
}
}
}
return foundBlocksToRemove;
}
private void SearchForMatches()
{
//Search the whole grid - look for matches in each direction and set the deleted flag for anything you find
for (int column = 0; column < Klax.NumberOfColumns; column++)
{
for (int row = 0; row < _grid[column].Count; row++)
{
SearchForMatches(column, row, 1, 0); //Horizontal
SearchForMatches(column, row, 0, 1); //Vertical
SearchForMatches(column, row, 1, 1); //Diagonal one way
SearchForMatches(column, row, 1, -1); //Diagonal the other way
}
}
}
private void SearchForMatches(int column, int row, int xStep, int yStep)
{
int startColorIndex = _grid[column][row].ColorIndex;
int matchCount = 0;
int x = column;
int y = row;
//Loop in the given direction till we run out of color or grid
do
{
matchCount++;
x += xStep;
y += yStep;
} while (x < Klax.NumberOfColumns && y < _grid[x].Count && y>0 && startColorIndex == _grid[x][y].ColorIndex);
//If the match is long enough
if (matchCount >= 3)
{
//Then mark all those blocks for removal
x = column;
y = row;
//Loop in the given direction till we run out of color or grid
do
{
_grid[x][y].Delete = true;
x += xStep;
y += yStep;
soundEffectInstance.Play();
} while (x < Klax.NumberOfColumns && y < _grid[x].Count && startColorIndex == _grid[x][y].ColorIndex);
}
}
private void ResetDeleteFlags()
{
//Mark all blocks in the grid as unmatched
for (int column = 0; column < Klax.NumberOfColumns; column++)
{
for (int row = 0; row < _grid[column].Count; row++)
{
_grid[column][row].Delete = false;
}
}
}
internal void Draw(SpriteBatch spriteBatch)
{
//Like the paddle the position of the block is inferred by its position in the grid
spriteBatch.Begin();
for (int column = 0; column < Klax.NumberOfColumns; column++)
{
for (int row = 0; row < _grid[column].Count; row++)
{
_grid[column][row].Draw(spriteBatch, column, GridOrigin - row * Klax.Texture.Height);
}
}
spriteBatch.End();
}
internal void Add(int column, Block block)
{
_grid[column].Add(block);
//Search for matches
do
{
ResetDeleteFlags();
SearchForMatches(); //Look for matches and mark their deleted flag
} while (RemoveBlocks()); //If we removed any then iterate again as there may be new matches
}
public bool HasSpace(int column)
{
//Is there room in this column
return _grid[column].Count < Klax.NumberOfRows;
}
}
}
На каком языке, пожалуйста, добавьте это к вашему тегу – Rafael
Извините. Новое для всего этого. Это моя первая публикация. Это C# –
Я думаю, что сообщение об ошибке довольно очевидно, 'soundEffectInstance' равно null. Вы можете проверить, почему 'soundEffect.CreateInstance()' возвращает null, я считаю, что строка выше этого ('content.Load ....') является причиной. – Dan