2011-12-18 57 views
0

錯誤:無法訪問處置的對象。對象名稱:'Texture2D'c#XNA無法訪問處置的對象。對象名稱:'Texture2D'。但即時通訊不處理

爲什麼會發生這種情況!我的代碼中沒有任何代碼告訴它處理! 這發生在我的XNA c#遊戲中的LoadContent()上。 代碼,我得到這個錯誤,ive突出顯示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; 
     // 
    } 
+0

你在ContentManager上調用Dispose()嗎?你使用某種定製的內容處理器/加載器嗎?你是否Dispose()其他任何東西,比如Model?你的程序中是否有*任何* Dispose(),並且可以調用該紋理?另外,錯誤消息的堆棧跟蹤是什麼?我看到的只是加載代碼 - 它在那裏失敗了嗎?你第一次運行代碼,還是將來有一段時間? – 2011-12-18 22:25:23

+0

我永遠不會調用Dispose(),我不使用自定義內容處理器/加載器。 我怎樣才能得到堆棧跟蹤? – 2011-12-18 22:35:25

+0

當您嘗試加載Texture2D時,是否拋出異常?什麼是例外?你可以在一個try ... catch中包含LoadContent中的代碼,並向我們發送被捕獲到的異常。 – 2011-12-19 06:44:48

回答

0

skyDomeTexture = Content.Load( 「紋理\天空盒」);

嘗試這種

skyDomeTexture = Content.Load(@ 「紋理/天空盒」);

+0

你是逐字字符串文字應該仍然有一個反斜槓。第一個例子會將字符串作爲空格來轉義,而第二個例子不會試圖轉義字符串。 – Phill 2011-12-21 03:04:22

相關問題