C# XNA文件未找到问题-指定的设置正确(即,这应该可以正常工作)
我有下面的Game1类,我对它做了一些修改(呵呵,这可能是它不能正常工作的原因…): 游戏1类C# XNA文件未找到问题-指定的设置正确(即,这应该可以正常工作),c#,file-io,xna,sprite,filenotfoundexception,C#,File Io,Xna,Sprite,Filenotfoundexception,我有下面的Game1类,我对它做了一些修改(呵呵,这可能是它不能正常工作的原因…): 游戏1类 public class Game1 : Microsoft.Xna.Framework.Game { //GraphicsDeviceManager graphics; SpriteController _spriteController; GraphicsDevice graphicsDevice; ContentManage
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);
}
}
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);
}
public Sprite(float x, float y, string assetName, ContentManager contentManager)
{
_position = new Vector2(x, y);
_texture = contentManager.Load<Texture2D>(assetName); //Error: file not found
}
来自精灵类
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);
}
}
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);
}
public Sprite(float x, float y, string assetName, ContentManager contentManager)
{
_position = new Vector2(x, y);
_texture = contentManager.Load<Texture2D>(assetName); //Error: file not found
}
公共精灵(浮点x、浮点y、字符串assetName、ContentManager)
{
_位置=新矢量2(x,y);
_texture=contentManager.Load(assetName);//错误:找不到文件
}
错误就发生在这里:我需要引用一个.png文件(它加载在外部内容文件中,具有相同的名称,并且有一个纹理的导入器/处理器
),但它没有加载。如图所示,fileAsset
名称为“pacmansprite”
有没有人能看出我做错了什么?如果需要,我会发布更多的代码,但现在我认为这应该足够了。我在您的代码中看到的第一个错误,与您的异常无关:您确实应该在
LoadContent
中加载内容,而不是在Draw
中加载内容。特别是考虑到您正在每一帧创建一个新的精灵对象。您应该考虑完全放弃SpRITE控制器和精灵类——它们似乎是不必要的间接层。
下一个错误:您设置自己的\u contentManager
的方式似乎是错误的。如果要加载纹理,则传入的服务容器需要包含GraphicsDeviceService
。您应该输入Game.Services
,或者,更好的做法是使用Game.Content
而不是创建自己的内容
最后,你的例外:你没有提供足够的信息来确定你得到它的确切原因。但以下是如何找出问题所在:
首先,您需要找出它试图从中加载纹理的路径。这是RootDirectory
和资产名称的组合,加上.xnb
文件扩展名。在我看来,它似乎正在尝试加载Content\pacmansprite.xnb
因此,下一步是查看构建目录(bin\x86\Debug
或类似目录)
并查看该路径是否存在文件。如果没有,则需要修改内容项目,以便内容生成输出显示在那里
(游戏项目需要引用内容项目,并且需要正确设置其根目录属性。资源需要具有正确的资产名称属性(以及其他设置)。)
如果遇到问题,可以尝试使用默认模板创建新项目,并直接加载和绘制资源。然后从那里开始工作。谢谢你的帮助。我很感激你提供的信息,我会接受你的建议。