c# XNA无法访问已处置的对象.对象名称:'Texture2D'但我不会处理它

本文关键字:对象 Texture2D 处理 访问 XNA | 更新日期: 2023-09-27 18:18:18

错误:无法访问已处置的对象。对象名称:'Texture2D'

为什么会这样?在我的整个代码中,没有任何地方告诉它要dispose!这是发生在我的XNA c#游戏的LoadContent()。我得到这个错误的代码,我高亮了Texture2D发生这种情况。

 protected override void LoadContent()
    {
        SoundEffect.DistanceScale = 20;
        SetUpPlain();
        SetUpWalls();
        // Walls
        wallsEffect = new BasicEffect(graphics.GraphicsDevice);
        wallsEffect.World = Matrix.Identity;
        wallsEffect.Projection = vars.projection;
        wallsEffect.TextureEnabled = true;
        wallsEffect.Texture = Content.Load<**Texture2D**>("Textures''Fence");
        // Set-up Player Options
        Player1.PlayerModel = Content.Load<Model>("PlayerA");
        Player1.BoundingSphere = new BoundingSphere(new Vector3(Player1.Position.X, Player1.Position.Y - 2, Player1.Position.Z), Player1.PlayerModel.Meshes[0].BoundingSphere.Radius * 0.04f);
        Player2.PlayerModel = Content.Load<Model>("PlayerA");
        Player2.BoundingSphere = new BoundingSphere(new Vector3(Player2.Position.X, Player2.Position.Y - 2, Player2.Position.Z), Player2.PlayerModel.Meshes[0].BoundingSphere.Radius * 0.04f);
        //
        loadStaticModel(Content, "Radio", null, 11, 4, -2, 0, 180, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 10, 1, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 12, 1, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 11, 3, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Forklift", null, 15, 0, -5, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "PepsiMachine", null, 25, 0, -5, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Chest", null, 10, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "PalletBoard", "Cork", 10, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Table", "Cork", 15, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Cylinder", "Cork", 15, 0, -40, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 184, 0, -196, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "WallTop", "Cork", 184, 0, -188, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 184, 0, -180, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Door1", "MetalA", 184, 0, -188, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Door1", "MetalA", 184, 0, -184, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 188, 0, -176.15f, 0, 90, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 196, 0, -176.15f, 0, 90, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        for (int i = 1; i <= MapSize / 8; i++)
        {
            loadStaticModel(Content, "Fence", "Fence", i * 8.12f - 4, 0, 0, 0, 270, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", 0, 0, -(i * 8.12f - 4), 0, 180, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", MapSize, 0, -(i * 8.12f - 4), 0, 0, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", i * 8.12f - 4, 0, -MapSize, 0, 90, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
        }
        // Sound
        vars.soundEffect[0] = Content.Load<SoundEffect>("Sounds''NormalStep");
        vars.soundEffect[1] = Content.Load<SoundEffect>("Sounds''Enter Sandman");
        vars.soundEffect[2] = Content.Load<SoundEffect>("Sounds''PlayerJoinGame");
        vars.soundEffect[3] = Content.Load<SoundEffect>("Sounds''JumpLand");
        vars.soundEffectInstance[0] = vars.soundEffect[1].CreateInstance();
        soundEffectPosition[0] = new Vector3(0, 200, 200);
        soundEffectEmitter[0] = new AudioEmitter();
        soundEffectEmitter[0].Position = soundEffectPosition[1];
        soundEffectListener[0] = new AudioListener();
        soundEffectListener[0].Position = Player1.Position;
        vars.soundEffectInstance[0].Apply3D(soundEffectListener[0], soundEffectEmitter[0]);
        vars.isSoundPlaying[0] = false;
        // Menu's
        vars.menuBackground = Content.Load<Texture2D>("Textures''menuBackground");
        PauseMenu.screenShotHighlight = Content.Load<Texture2D>("Textures''ScreenShotViewerHighlight");
        vars.MenuFont = Content.Load<SpriteFont>("Fonts//MenuFont");
        // Death HUD
        deathBackground = Content.Load<Texture2D>("Textures''deathBackground");
        //
        chatBackground = Content.Load<Texture2D>("Textures''chatBackground");
        plainTexture = Content.Load<Texture2D>("Textures''Grass");
        // Tank
        tank = Content.Load<Model>("tank");
        tankTransforms = new Matrix[tank.Bones.Count];
        // Sky Dome
        skyDome = Content.Load<Model>("SkyDome");
        skyDomeTexture = Content.Load<Texture2D>("Textures''skyBox");
        skyDomeTransforms = new Matrix[skyDome.Bones.Count];
        //
        font = Content.Load<SpriteFont>("Fonts//PCFont1");
        chatFont = Content.Load<SpriteFont>("Fonts//chatFont");
        xboxButtons = Content.Load<SpriteFont>("Fonts//xboxControllerSpriteFont");
        xboxFont = Content.Load<SpriteFont>("Fonts//MenuFont");
        spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
        TextureEffect = Content.Load<Effect>("TextureEffect");
        // Viewports
        leftViewport = GraphicsDevice.Viewport;
        rightViewport = GraphicsDevice.Viewport;
        leftViewport.Width = leftViewport.Width / 2;
        rightViewport.Width = rightViewport.Width / 2;
        rightViewport.X = leftViewport.Width;
        // 
    }

c# XNA无法访问已处置的对象.对象名称:'Texture2D'但我不会处理它

skyDomeTexture = Content.Load("Textures'skyBox");

试试这个

skyDomeTexture = Content.Load(@"Textures/skyBox");