Problem Loading Textures via DrawableGameComponent Implementation
I get a ContentLoadException "File not found", when the debugger hits my LoadContent method in my DrawableGameComponent. I created a test string that outputs the Content Root Directory and it is as follows : \GameName\bin\x86\Debug\Content minus my personal folders preceding it of course.
Here is the code in the Game child class :
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();
}
And the DrawableGameComponent implementation :
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 + 开发者_StackOverflow中文版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();
}
I've checked and double checked the Folders, Filenames, etc and they all look normal. I'm absolutely stumped.
Solved it. My Character was being added to the Component list in the Games Initialize() before it's super class call to base.Initialize(). This made my game begin calling the Character's Load and Init functions. Since the Game's Init was not called, the super class Microsoft.Xna.Framework.Game Content variable was either a null pointer or not set up properly.
The solution was to add the Character to the Component list in the game's LoadContent()
精彩评论