下面的代码给了我一个ObjectDisposedException。我在内容加载器中加载内容就好了。它不时有效,但偶尔也会抛出异常。
所有纹理都完美加载,每个对象都有正确的纹理并且实际存在。我无法卸载内容。
最后一个spriteBatch.End();
抛出错误出了什么问题?
protected override void Draw(GameTime gameTime)
{
if (startMenu.showMenu)
{
spriteBatch.Begin();
startMenu.Draw(spriteBatch);
spriteBatch.End();
}
if (selectedLevel != null && !startMenu.showMenu)
{
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null,
selectedLevel.GetCamera().getViewMatrix(new Vector2(1.0f)));
for (int layer_inc = 0; layer_inc <= selectedLevel.layers.Count() - 1; layer_inc++)
{
selectedLevel.layers[layer_inc].Draw(gameTime, spriteBatch);
}
if (selectedLevel.GetPlayer().IsAlive())
{
Weapon weapon = selectedLevel.GetPlayer().GetWeapon();
weapon.Draw(gameTime, spriteBatch);
if (selectedLevel.GetPlayer().isMeditating())
{
Player player = selectedLevel.GetPlayer();
MeditationBoost meditationBoost = ((MeditationBoost)player.getBoost());
meditationBoost.Draw(spriteBatch, player.position);
}
if (weapon is ShootableWeapon)
{
ShootableWeapon shootableWeapon = (ShootableWeapon)weapon;
if (shootableWeapon.getAmmo().Count() > 0)
{
for (int ammo_inc = 0; ammo_inc <= shootableWeapon.getAmmo().Count() - 1; ammo_inc++)
{
shootableWeapon.getAmmo()[ammo_inc].Draw(gameTime, spriteBatch);
}
}
}
}
foreach (MovableObject movableObject in selectedLevel.movableObjects)
{
if (movableObject is Enemy)
{
Enemy enemy = (Enemy)movableObject;
ThrowAttack attack = ((ThrowAttack)enemy.getAttack());
if (attack != null && attack.getThrowObject() != null)
{
attack.getThrowObject().Walk(Direction.Right, attack.getThrowObject().speed);
attack.getThrowObject().Update(gameTime);
attack.getThrowObject().Draw(spriteBatch);
}
}
}
updateScreenText(spriteBatch);
try
{
spriteBatch.End();
}
catch (ObjectDisposedException e)
{
Debug.Write(e);
}
}
base.Draw(gameTime);
}
编辑:
这是我动态加载内容的方式(我在使用此函数时将文件夹作为字符串提供)
public void Loadlistontent(string contentFolder)
{
DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "/" + contentFolder);
if (!dir.Exists)
throw new DirectoryNotFoundException();
FileInfo[] files = dir.GetFiles("*.*");
foreach (FileInfo file in files)
{
if (!textureLoader.GetTextures().ContainsKey(file.Name))
{
string key = Path.GetFileNameWithoutExtension(file.Name);
Texture2D texture = Content.Load<Texture2D>(contentFolder + "/" + key);
textureLoader.addTexture(file.Name, texture);
}
}
}
答案 0 :(得分:0)
在代码的任何点,您是否呼叫Content.Unload()
或Texture2D.Dispose()
?
似乎其中一个可能隐藏在Spritebatch.Begin()
和Spritebatch.End()
之间。乔可能会在updateScreenText()
中提到。
在Spritebatch.End()
抛出异常的原因是因为spritebatch将你的精灵“批处理”在一起进行一次绘制调用。但是,在那个单一的抽奖调用中,所有被处置内容的例外都将被抛出,因为那是他们实际使用的时候。