2013-11-15 36 views
0

我是XNA中的一名相對初學者,並且正在嘗試爲C#XNA遊戲創建一個screenManager。我的代碼中的所有內容似乎都能正常工作,直到我在MainMenu類中的LoadContent方法中產生自Screen這是一個自定義獨立類。這是我得到我的GraphicsDevice組件未發現ContentLoad異常。xna從非GameComponent類中加載內容

這是在聲明:texture = c.Load<Texture2D>("Textures/Start"); 其中c是ContentManager

這個聲明和程序工作正常,如果我把它放在我的課程的更新方法。但是,我只想加載一次該紋理,而不是每次調用Update(GameTime)。我想我必須在內容信息初始化之前調用加載,但我不確定是什麼。在MainMenu.LoadContent中有一些註釋掉的語句幾乎完全相同,都會產生相同的錯誤。我在這裏發佈我的整個代碼。希望它不會淹沒論壇。 這裏的Game1.cs:

namespace RoomGame 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     public static GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     GameController gameController; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 

      gameController = new GameController(this,this.Content); 
      this.Components.Add(gameController); 
     } 

    } 
} 

GameController.cs:

namespace RoomGame 
{ 
    public class GameController : DrawableGameComponent 
    { 
     public Game game; 
     public ContentManager content; 
     public ScreenController screenController; 
     public MainMenu menu; 
     public GameController(Game game,ContentManager c) 
      : base(game) 
     { 
      this.game = game; 
      content = c; 
      screenController = new ScreenController(this); 
      menu = new MainMenu(this,this.content); 
      screenController.AddScreen(menu); 
     } 

     public override void Initialize() 
     { 
      base.Initialize(); 
      screenController.Initialize(); 
     } 

     protected override void LoadContent() 
     { 
      base.LoadContent(); 
     } 

     protected override void UnloadContent() 
     { 
      base.UnloadContent(); 
     } 

     public override void Update(GameTime gameTime) 
     { 
      screenController.Update(gameTime); 
      base.Update(gameTime); 
     } 

     public override void Draw(GameTime gameTime) 
     { 
      base.Draw(gameTime); 
      screenController.Draw(gameTime); 
     } 
    } 
} 

screenController.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace RoomGame 
{ 

    public class ScreenController 
    { 
     public List<Screen> screens = new List<Screen>(); 
     GameController gameController; 
     SpriteBatch spriteBatch; 

     public ScreenController(GameController gc) 
     { 
      this.gameController = gc; 
     } 

     public void Initialize() 
     { 

      spriteBatch = new SpriteBatch(this.gameController.GraphicsDevice); 
     } 

     public void Update(GameTime gameTime) 
     { 
      foreach (Screen screen in screens) 
      { 
       if (screen.isUpdating) 
        screen.Update(gameTime); 
      } 
     } 

     public void Draw(GameTime gameTime) 
     { 
      foreach (Screen screen in screens) 
      { 
       if (screen.isDrawn) 
       { 
        spriteBatch.Begin(); 
        screen.Draw(gameTime,spriteBatch); 
        spriteBatch.End(); 
       } 
      } 
     } 

     public void AddScreen(Screen screen) 
     { 
      if (!screens.Contains(screen)) 
      { 
       screen.LoadContent(this.gameController.game.Content); 
       screens.Add(screen); 
       //screen.Initialize(); 

      } 
     } 




    } 
} 

Screen.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Content; 

namespace RoomGame 
{ 
    public class Screen 
    { 
     public ScreenController screenController; 
     public bool isUpdating=true; 
     public bool isDrawn=true; 
     public GraphicsDeviceManager graphics; 

     /*public Screen(ScreenController sc) 
     { 
      this.screenController = sc; 
      this.isActive = true; 
     }*/ 

     public virtual void Initialize() 
     { } 

     public virtual void LoadContent(ContentManager c) 
     { } 


     public virtual void Update(GameTime gameTime) 
     { } 

     public virtual void Draw(GameTime gameTime,SpriteBatch sb) 
     { } 
    } 
} 

MainMenu.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Content; 

namespace RoomGame 
{ 
    public class MainMenu : Screen 
    { 
     GameController gameController; 
     Texture2D texture; 
     Vector2 position; 
     ContentManager content; 
     //public GraphicsDeviceManager graphics; 

     public MainMenu(GameController gc,ContentManager c) 
     { 
      this.gameController = gc; 
      this.content = c; 
      this.isUpdating = true; 
      this.isDrawn = true; 
      //this.graphics = this.gameController.game.graphics; 
     } 

     public override void LoadContent(ContentManager c) 
     { 

      texture = c.Load<Texture2D>("Textures/Start"); 
      //texture = this.gameController.game.Content.Load<Texture2D>("Textures/Start"); 
      //texture=this.gameController.content.Load<Texture2D>("Textures/Start"); 
      //texture = this.gameController.Game.Content.Load<Texture2D>("Textures/Start"); 
     } 


     public override void Update(GameTime gameTime) 
     { 
      //texture = this.gameController.content.Load<Texture2D>("Textures/Start"); 
      position = new Vector2(100, 100); 
     } 

     public override void Draw(GameTime gameTime,SpriteBatch sb) 
     { 
      sb.Draw(texture, position, Color.White); 
     } 
    } 
} 

在此先感謝....

編輯
我發現我的錯誤。我在GameControllerLoadContent之前撥打ScreenController.Add。我把它放在base.LoadContentgameController之後,現在就起作用了。我希望這可以幫助某人,但如果有人知道更好的方法來處理這件事,請讓我知道。

+0

請不要在下次發佈所有代碼。 – pinckerman

回答

0

您可以從ScreenController的構造函數中調用MainMenu.LoadContent,而構造函數又由Game's構造函數調用。應在適當的時間調用LoadContent。因此請將電話AddScreen移至GameControllerLoadContent方法。