c# - 无法访问已处置的对象。对象名称:'Icon',当我所做的只是生成平铺精灵时,MonoGame 中随机出错
问题描述
我是第一次学习 MonoGame,我制作了一个系统,可以生成地图字典并在玩家附近生成图块,并在它们超出范围时将其移除。
这似乎按预期工作,但是在我走了一会儿之后,我得到了这个错误:
System.ObjectDisposedException: '无法访问已处置的对象。对象名称:“图标”。
貌似没有图案。它显示在行:
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:\Users\User\Desktop\Coding\C# Projects\MonoGame\FalseGold\FalseGold\Sprites.cs:line 226
at ThisGame.Functions.LoadTiles() in C:\Users\User\Desktop\Coding\C# Projects\MonoGame\FalseGold\FalseGold\Functions.cs:line 80
at ThisGame.Sprites.Update() in C:\Users\User\Desktop\Coding\C# Projects\MonoGame\FalseGold\FalseGold\Sprites.cs:line 30
at ThisGame.MainGame.Update(GameTime gameTime) in C:\Users\User\Desktop\Coding\C# Projects\MonoGame\FalseGold\FalseGold\Game.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:\Users\User\Desktop\Coding\C# Projects\MonoGame\FalseGold\FalseGold\Program.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
该类的析构函数会破坏图形管理器对象,从而使指向内部绘图结构的指针无效。
: Game
从除 之外的所有类的末尾删除MainGame
。这应该可以解决您的问题。
推荐阅读
- java - Rider 在 Mac 上使用的 java.security 文件的位置
- asp.net-core - 将服务器端数据嵌入到 ASP.NET Core 中的 JavaScript 文件中
- c++ - 为什么可以访问 std::unique_ptr * u1 作为 u1[1000] 并且它仍然有效
- sql - 我必须解决的当前问题的 SQL SELECT 问题
- javascript - 我不知道如何在 discord.js 中执行可配置的命令
- java - 如何在 CODENAME ONE 的表格中的每一行上添加一个按钮?
- arrays - 新手:在结构中引用和初始化结构元素
- python - 如何在 python 中重新排列 Dataframe 结构
- html - 使用css内联块时如何将项目与顶部对齐?
- python - 插入时Python Sqlite问号问题