2011-05-13 29 views
0

當調試器在我的DrawableGameComponent中點擊我的LoadContent方法時,我得到一個ContentLoadException「文件未找到」。我創建了一個輸出內容根目錄的測試字符串,它如下所示:\ GameName \ bin \ x86 \ Debug \ Content減去我之前的個人文件夾。通過DrawableGameComponent加載紋理的問題實現

這裏是遊戲子類代碼:

GraphicsDeviceManager graphics; 
global_vars variables; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; //Folder for the Content Manager to place pipelined files as they are loaded 
    } 

    /// <summary> 
    /// Allows the game to perform any initialization it needs to before starting to run. 
    /// This is where it can query for any required services and load any non-graphic 
    /// related content. Calling base.Initialize will enumerate through any components 
    /// and initialize them as well. 
    /// </summary> 
    protected override void Initialize() 
    { 
     variables = new global_vars(graphics); 
     Character c = new Character(null, null, variables, this); 
     this.Components.Add(c); 
     base.Initialize(); 
    } 

而且DrawableGameComponent實現:

public Character(Ability[] starting_abilities, Player owner, global_vars vars, Game game) : base(game) 
    { 
     this.variables = vars; 
     this.abilities = starting_abilities; 
     this.character_owner = owner; 
     this.experience = 0; 
     this.position = new Rectangle(variables.CHARACTER_START_POSITION_X, variables.CHARACTER_START_POSITION_Y, variables.CHARACTER_WIDTH + variables.CHARACTER_START_POSITION_X, variables.CHARACTER_HEIGHT + variables.CHARACTER_START_POSITION_Y); 
    } 

    public override void Initialize() 
    { 
     base.UpdateOrder = variables.CHARACTER_UPDATE_PRIORITY; 
     base.DrawOrder = variables.CHARACTER_UPDATE_PRIORITY; 
     base.Enabled = true; //Enables Game to call Update on this component 
     base.Visible = true; //Enables Game to call Draw on this component 

     this.move_speed = 3; 
     this.position.X = variables.CHARACTER_START_POSITION_X; 
     this.position.Y = variables.CHARACTER_START_POSITION_Y; 
     this.move_state = variables.CHARACTER_DEFAULT_MOVESTATE; 
     this.charsprite = new SpriteBatch(variables.manager.GraphicsDevice); 

     base.Initialize();  //Super class calls LoadContent 
    } 

    protected override void LoadContent() 
    { 
     String test = Game.Content.RootDirectory; 
     character_default = Game.Content.Load<Texture2D>("Character_Grey_Eyes_Center"); 
     character_right = Game.Content.Load<Texture2D>("Character_Grey_Eyes_Right"); 
     character_left = Game.Content.Load<Texture2D>("Character_Grey_Eyes_Left"); 
     character_down = Game.Content.Load<Texture2D>("Character_Grey_Eyes_Down"); 
     character_up = Game.Content.Load<Texture2D>("Character_Grey_Eyes_Up"); 

     base.LoadContent(); 
    } 

我檢查和雙重檢查的文件夾,文件名等,他們都期待正常。我絕對難住。

+0

在你的內容項目,你有你的紋理在紋理文件夾? – 2011-05-13 05:32:02

+0

不,他們都在內容文件夾 – xdevient 2011-05-13 21:26:44

回答

0

解決了它。我的角色在被超類調用base.Initialize()之前被添加到Games Initialize()中的Component列表中。這使我的遊戲開始調用Character的Load和Init函數。由於Game的Init沒有被調用,所以超類Microsoft.Xna.Framework.Game Content變量是空指針或者沒有正確設置。

的解決方案是將字符添加到組件列表中游戲的LoadContent()

+0

我想我沒有注意到執行過程中調試器所處的類文件 – xdevient 2011-05-13 22:15:25