通过DrawableGameComponent实现加载纹理的问题

时间:2011-05-13 02:04:54

标签: file xna load textures

当调试器在我的DrawableGameComponent中遇到我的LoadContent方法时,我得到一个ContentLoadException“File not found”。我创建了一个输出内容根目录的测试字符串,它如下所示:\ GameName \ bin \ x86 \ Debug \ Content当然减去我前面的个人文件夹。

以下是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();
    }

我已经检查并仔细检查了文件夹,文件名等,它们看起来都很正常。我绝对难过。

1 个答案:

答案 0 :(得分:0)

解决了它。我的角色被添加到Games Initialize()的Component列表中,然后才调用base.Initialize()。这使我的游戏开始调用Character的Load和Init函数。由于没有调用Game的Init,超类Microsoft.Xna.Framework.Game Content变量要么是空指针,要么没有正确设置。

解决方案是将角色添加到游戏的LoadContent()

中的组件列表中