最近我接手MonoGame并开始学习3D游戏编程,在一些关于如何添加3d模型的在线教程之后我得到了以下代码,它应该显示一个3d立方体和我控制的摄像头来平移它:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
Vector3 camTarget;
Vector3 camPosition;
Matrix projectionMatrix;
Matrix viewMatrix;
Matrix worldMatrix;
Model model;
// orbit
bool orbit;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
camTarget = new Vector3(0f, 0f, 0f);
camPosition = new Vector3(0f, 0f, -100f);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), GraphicsDevice.DisplayMode.AspectRatio, 1f, 1000f);
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
worldMatrix = Matrix.CreateWorld(camTarget, Vector3.Forward, Vector3.Up);
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
model = Content.Load<Model>("MonoCube");
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
camPosition.X -= 1f;
// remove this to create a rotation
camTarget.X -= 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
camPosition.X += 1f;
// remove this to create a rotation
camTarget.X += 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
camPosition.Y -= 1f;
// remove this to create a rotation
camTarget.Y -= 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
camPosition.Y += 1f;
// remove this to create a rotation
camTarget.Y += 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemPlus))
{
camPosition.X += 1f;
// remove this to create a rotation
}
if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
{
camPosition.X -= 1f;
// remove this to create a rotation
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
orbit = !orbit;
}
if (orbit)
{
Matrix rotationMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(1f));
camPosition = Vector3.Transform(camPosition, rotationMatrix);
}
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
effect.View = viewMatrix;
effect.World = worldMatrix;
effect.Projection = projectionMatrix;
//mesh.Draw();
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
问题在于,当我尝试开始游戏时,我收到以下错误:
MonoGame.Framework.dll中发生未处理的“Microsoft.Xna.Framework.Content.ContentLoadException”类型异常 其他信息:无法将MonoCube资源加载为非内容文件!
我无法弄清楚问题是什么,管道有.dae文件和模型的png并且构建正常,内容管道.mgcb文件在Content文件夹中,所以这不是问题我知道,我真的很感激任何和所有的帮助,因为我现在对此不知所措。
答案 0 :(得分:1)
您可以尝试使用XNB文件来确保加载没有任何问题。微软有一些例子,这是第一个出现在我身上的例子: http://xbox.create.msdn.com/en-US/education/catalog/lab/marble_maze
可能是一个愚蠢的建议,但要确保编译的内容文件位于主项目的Content文件夹中,而不是内容项目。
您定位的是什么平台? Haven一段时间没有使用monogame,但之前你不能在ios上使用Spritefont编译为windows,反之亦然。查看内容项目中.dae文件的选项。