无法访问已释放的对象。对象名称:"图标",当我所做的只是生成瓷砖精灵时,在 MonoGame 中随机出错



我是第一次学习MonoGame,我制作了一个系统,可以生成地图字典,在玩家附近生成瓷砖,并在瓷砖超出范围时将其移除。

这似乎如预期的那样工作,但在我四处走动一段时间后,我得到了这个错误:

System.ObjectDisposedException:'无法访问已释放的对象。对象名称:'Icon'。'

似乎没有任何模式。它显示在线路上:

public Tile(int X, int Y, string Type)

我是面向对象编程的新手,但无法理解在线错误的原因,因为它们似乎无关。如果需要更多信息,请询问,我会添加。我将非常感谢您的帮助。

瓷砖精灵:

//##Tile##
public class Tile : Game
{
//##Default Varibles##
public Vector2 Scale;
public Vector2 Position;
public Rectangle Rect;
//##Initialise##
public Vector2 GridPosition;
public Tile(int X, int Y, string Type)
{
Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
//Position
GridPosition = new Vector2(X, Y);
Position = new Vector2(MainGame.instance.Graphics.PreferredBackBufferWidth / 2 - MainGame.instance.AssetManager.TileTextureList[0].Width / 2 + X * 64, MainGame.instance.Graphics.PreferredBackBufferHeight / 2 - MainGame.instance.AssetManager.TileTextureList[0].Height / 2 + Y * 64);
//Rect
Rect = new Rectangle((int)Position.X, (int)Position.Y, MainGame.instance.AssetManager.TileTextureList[0].Width, MainGame.instance.AssetManager.TileTextureList[0].Height);
}
//##EveryFrame##
public void Update()
{
//Function Calls

//Update Rectangle
Rect.Location = new Point((int)Position.X, (int)Position.Y);
}
//##Draw##
public void Draw()
{
MainGame.instance.MainLayer.Draw(texture: MainGame.instance.AssetManager.TileTextureList[0], position: Position, color: Color.White);
}
}

堆栈跟踪我认为:

StackTrace: '   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at ThisGame.Sprites.Tile..ctor(Int32 X, Int32 Y, String Type) in C:UsersUserDesktopCodingC# ProjectsMonoGameFalseGoldFalseGoldSprites.cs:line 226
at ThisGame.Functions.LoadTiles() in C:UsersUserDesktopCodingC# ProjectsMonoGameFalseGoldFalseGoldFunctions.cs:line 80
at ThisGame.Sprites.Update() in C:UsersUserDesktopCodingC# ProjectsMonoGameFalseGoldFalseGoldSprites.cs:line 30
at ThisGame.MainGame.Update(GameTime gameTime) in C:UsersUserDesktopCodingC# ProjectsMonoGameFalseGoldFalseGoldGame.cs:line 101
at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime)
at Microsoft.Xna.Framework.Game.Tick()
at MonoGame.Framework.WinFormsGameWindow.TickOnIdle(Object sender, EventArgs e)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MonoGame.Framework.WinFormsGameWindow.RunLoop()
at MonoGame.Framework.WinFormsGamePlatform.RunLoop()
at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
at Microsoft.Xna.Framework.Game.Run()
at ThisGame.Program.Main() in C:UsersUserDesktopCodingC# ProjectsMonoGameFalseGoldFalseGoldProgram.cs:line 12'
Exception thrown: 'System.ObjectDisposedException' in System.Drawing.dll
An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Drawing.dll
Cannot access a disposed object.

游戏正在运行:链接

如果我不四处走走,它就不会崩溃。

看起来您正在创建内部game类的多个实例。

当其中一个超出范围时,game类的析构函数会破坏图形管理器对象,从而使指向内部图形结构的指针无效。

从除MainGame之外的所有类的末尾删除: Game。这应该可以解决您的问题。

最新更新