首页 > 解决方案 > 无法访问已处置的对象。对象名称:'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.

游戏运行: 链接

如果我不走动一下,它就不会崩溃。

标签: c#c#-4.0monogame

解决方案


我似乎正在创建内部game类的多个实例。

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

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


推荐阅读