问题描述
我正在尝试加载一个精灵图像文件 (xnb),但它找不到该文件。我尝试设置自定义根目录,我尝试将文件放入内容中,但仍然找不到。这是我的代码。
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Linq;
namespace screen_switch
{
public class Game1 : Microsoft.Xna.Framework.Game
{
// graphics
public GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
//page
public PageManager PageMgr = new PageManager();
public PageGame pageGame = new PageGame();
public PageGame2 pageGame2 = new PageGame2();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
//init pages
PageMgr.Add(pageGame,this);
PageMgr.Add(pageGame2,this);
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch,which can be used to draw textures.
this.IsMouseVisible = true;
spriteBatch = new SpriteBatch(GraphicsDevice);
// Todo: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// Todo: Unload any non ContentManager content here
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
PageMgr.Update(gameTime,this);
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)
{
KeyboardState kS = Microsoft.Xna.Framework.Input.Keyboard.GetState();
if (kS.IsKeyDown(Keys.Up))
{
PageMgr.Set(pageGame2,this);
}
if (kS.IsKeyDown(Keys.Down))
{
PageMgr.Set(pageGame,this);
}
GraphicsDevice.Clear(Color.CornflowerBlue);
// Todo: Add your drawing code here
PageMgr.Draw(this);
spriteBatch.Begin();
Texture2D squareTexture;
squareTexture = Content.Load<Texture2D>("square");
spriteBatch.Draw(squareTexture,new Vector2(100,100),Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
我真的不明白。错误信息是:
Microsoft.Xna.Framework.Content.ContentLoadException
HResult=0x80131500
Message=Error loading "square". File not found.
Source=Microsoft.Xna.Framework
StackTrace:
at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName,Action`1 recorddisposableObject)
at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
at screen_switch.Game1.Draw(GameTime gameTime) in C:\Users\Bigweld\source\repos\screen switch\screen switch\screen_switch\Game1.cs:line 88
at Microsoft.Xna.Framework.Game.DrawFrame()
at Microsoft.Xna.Framework.Game.Tick()
at Microsoft.Xna.Framework.Game.HostIdle(Object sender,EventArgs e)
at Microsoft.Xna.Framework.GameHost.OnIdle()
at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender,EventArgs e)
at System.Windows.Forms.Application.threadcontext.System.Windows.Forms.UnsafeNativeMethods.imsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.imsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)
at System.Windows.Forms.Application.threadcontext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.threadcontext.RunMessageLoop(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Microsoft.Xna.Framework.WindowsGameHost.Run()
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at screen_switch.Program.Main(String[] args) in C:\Users\Bigweld\source\repos\screen switch\screen switch\screen_switch\Program.cs:line 15
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
FileNotFoundException: Error loading "Content\square.xnb". File not found.
最终目标是加载我的方形精灵,并在屏幕的某个地方绘制它。我什至不在乎我只需要知道我可以在这个该死的框架中放置精灵的能力。请帮忙,谢谢!
解决方法
squareTexture = Content.Load<Texture2D>("square");
应在 LoadContent()
方法中定义。
并确保在解决方案资源管理器的内容文件夹中找到您的精灵。并且命名/目录是正确的。