无法访问已处置的对象。对象名称:“ Icon”,当我正在做的是生成图块精灵时,MonoGame中会随机发生错误

问题描述 投票:0回答:1

我是第一次学习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.

游戏正常运行:Link

如果我不走一会儿,它不会崩溃。

我是第一次学习MonoGame,我制作了一个系统,该系统生成地图字典并在玩家附近生成瓦片,并在超出范围时将其删除。这似乎以...

c# c#-4.0 monogame
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.