当调试器在我的DrawableGameComponent中命中我的LoadContent方法时,我得到一个ContentLoadException"File not found"。 我创建了一个输出内容根目录的测试字符串,如下所示:\游戏名称\bin\x86\调试\内容减去我前面的个人文件夹。
下面是 Game 子类中的代码:
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();
}
我已经检查并仔细检查了文件夹,文件名等,它们看起来都很正常。 我完全被难住了。
解决了。 我的角色在超级类调用基地之前被添加到游戏初始化()的组件列表中。初始化()。 这让我的游戏开始调用角色的加载和初始化函数。 由于未调用游戏的 Init,因此超类 Microsoft.Xna.Framework.Game Content 变量要么为空指针,要么未正确设置。
解决方案是将角色添加到游戏的 LoadContent() 中的组件列表中