XNA文件未找到问题 - 设置指定正确(即,这应该工作)

时间:2011-12-05 00:35:50

标签: c# file-io xna sprite filenotfoundexception

我有以下Game1类,我稍微改了一下(呵呵,实际上可能就是为什么它不能正常工作......):

Game1 Class

public class Game1 : Microsoft.Xna.Framework.Game
    {
        //GraphicsDeviceManager graphics;
        SpriteController _spriteController;
        GraphicsDevice graphicsDevice;
        ContentManager _contentManager;

        public Game1()
        {
            _contentManager = new ContentManager(new Chronos.Engine.Infrastructure.ServiceContainer());
            _contentManager.RootDirectory = "Content";
        }

        /// <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()
        {
            _spriteController = new SpriteController();



            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            _spriteController.GraphicsDeviceService.GraphicsDevice.Clear(Color.CornflowerBlue);

            string fileAsset = "pacmansprite";

            _spriteController.AddSprite(
               new Sprite(
                   10,
                   10,
                   fileAsset,
                   _contentManager
               ),
               fileAsset
           );

            _spriteController.Draw(10, 10, fileAsset);

            base.Draw(gameTime);
        }
    }

其他信息

精灵控制器只是一个包含多个精灵的类,根据指定的键绘制精灵(即,IDictionary<string, Sprite>用于包含它们)。在spriteController中是一个GraphicsDevice引用,可以访问它,以及可以返回的内容管理器。最后,我想让SpriteController成为Game1类中唯一的一个字段,因为它提供了所有必要的组件来完成渲染简单图形所需的内容,但在修复之前我不能这样做此

问题发生在Sprite类'构造函数中。这是传递数据的方法: 来自Game1 Class

protected override void Draw(GameTime gameTime)
            {
                _spriteController.GraphicsDeviceService.GraphicsDevice.Clear(Color.CornflowerBlue);

                string fileAsset = "pacmansprite";

                _spriteController.AddSprite(
                   new Sprite(
                       10,
                       10,
                       fileAsset,
                       _contentManager
                   ),
                   fileAsset
               );

                _spriteController.Draw(10, 10, fileAsset);

                base.Draw(gameTime);
            }

来自Sprite Class

public Sprite(float x, float y, string assetName, ContentManager contentManager)
        {
            _position = new Vector2(x, y);
            _texture = contentManager.Load<Texture2D>(assetName); //Error: file not found

        }

错误发生在这里:我需要引用一个.png文件(它在外部内容文件中加载,具有相同的名称,并且具有Texture的导入器/处理器),这不是加载。如图所示,fileAsset名称是“pacmansprite”。

有没有人可以看到我做错了什么?如果需要,我会发布更多代码,但是现在我认为这应该足够了。

1 个答案:

答案 0 :(得分:3)

我在您的代码中看到的第一个与您的例外无关的错误:您确实应该在LoadContent而非Draw中加载内容。特别是考虑到你每个帧都在创建一个新的精灵对象。你应该考虑完全放弃你的精灵控制器和精灵类 - 它们似乎是一个不必要的间接层。

下一个错误:您设置自己的_contentManager的方式似乎是错误的。如果要加载纹理,则传入的服务容器需要包含GraphicsDeviceService。您应该传递Game.Services,或者更好的是,只使用Game.Content而不是创建自己的!{/ p>

最后,您的例外:您没有提供足够的信息来确定您获得它的确切原因。但这里是如何找出错误的原因:

首先,您需要找出它尝试加载纹理的路径。这是RootDirectory和资产名称以及.xnb文件扩展名的组合。对我而言,它似乎正在尝试加载Content\pacmansprite.xnb

所以下一步是查看你的构建目录(bin\x86\Debug或类似的) 并查看该路径中是否存在文件。如果没有,您需要修改内容项目,以便在那里显示内容构建输出。

(内容项目需要由您的游戏项目引用,并且需要正确设置其根目录属性。资产需要具有正确的资产名称属性(以及其他设置)。)

如果您遇到问题,可以尝试使用默认模板创建新项目并直接加载和绘制资产。然后从那里开始工作。