我试图在游戏中匹配时插入声音。但是,当我得到匹配时,我不断收到NullReferenceException。它转到了soundEffectInstance.Play();行。任何帮助是极大的赞赏。在试图让它发挥作用时,我尝试了一切。这是我的代码:
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;
}
}
}
答案 0 :(得分:0)
从我在您的代码中看到的,您使用的是Microsoft.Xna.Framework框架中的ContentManager.Load。
在这种假设下,您的代码可能无法正常工作,并且始终会返回null。
这里的问题有两个:
ContentManager.Load仅支持其定义给出的特定类型(我将在下面发布一个链接)。 SoundEffects不属于这些类型
此加载方法认为所有给定的文件本身没有扩展名,而是具有ungiven .xnb扩展名(并且是xnb格式)。
因此你的Sounds / 24372 ^ pop2.mp3会转换为Sounds / 24372 ^ pop2.mp3.xnb。
直接从文档中使用它的一个例子:
Model model = contentManager.Load<Model>( ".\\content\\models\\box" );
这会将box.xnb加载为模型。
该方法的完整文档如下: https://msdn.microsoft.com/en-us/library/bb197848.aspx
就像我提到的,如果我假设你使用THAT方法是正确的那么很明显为什么加载失败,因为它正在寻找一个不存在的文件。但即使根据文档进行了更正,它仍然无法工作,因为它不是受支持的类型(虽然当你尝试使用它来加载不受支持但不支持的方法时该方法不会抛出异常,我觉得很奇怪开发人员疏忽与你的问题完全无关。)