【问题标题】:System.InvalidOperationException in WP 8 Game (MonoGame )WP 8 游戏 (MonoGame) 中的 System.InvalidOperationException
【发布时间】:2013-04-26 05:36:27
【问题描述】:

这是我的 Game1 课:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameName2
{
     public class Game1 : Game
     {
         GraphicsDeviceManager _graphics;
         SpriteBatch _spriteBatch;
         Texture2D _bg;

         public Game1()
         {
             _graphics = new GraphicsDeviceManager(this);
             Content.RootDirectory = "Content";
         }

         protected override void Initialize()
         {
             // TODO: Add your initialization logic here

             base.Initialize();
         }

         protected override void LoadContent()
         {
             // Create a new SpriteBatch, which can be used to draw textures.
             _spriteBatch = new SpriteBatch(GraphicsDevice);

             _bg = Content.Load<Texture2D>(@"Loading");
             // 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)
         {
             // TODO: Add your update logic here

             base.Update(gameTime);
         }


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

             // TODO: Add your drawing code here

             _spriteBatch.Draw(_bg,
                 new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
                 null,
                 Color.White,
                 0,
                 Vector2.Zero,
                 SpriteEffects.None,
                 0);

             base.Draw(gameTime);
         }
     }
 }

我在我的项目中创建了一个“内容”文件夹,并将 Loading.xnb 添加为现有项目。然后,我将 Loading.xnb 的 Build Action 更改为“Content”,将 Copy to Output 更改为“Copy Always”。

但是当我编译它时,这部分会抛出 System.InvalidOperationException

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

         // TODO: Add your drawing code here

         _spriteBatch.Draw(_bg,
             new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
             null,
             Color.White,
             0,
             Vector2.Zero,
             SpriteEffects.None,
             0);

         base.Draw(gameTime);
     }

特别是在 _spriteBatch.Draw(.......) 方法中。有人可以帮助我吗?谢谢。

【问题讨论】:

  • 请发布整个错误和堆栈跟踪。

标签: xna windows-phone-8 monogame content-pipeline


【解决方案1】:

查看一些示例程序。 _spriteBatch.Draw 必须在 _spriteBatch.Begin 和 _spriteBatch.End 之间调用。

     GraphicsDevice.Clear(Color.CornflowerBlue);

     // TODO: Add your drawing code here

     _spriteBatch.Begin();
     _spriteBatch.Draw(_bg,
         new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
         null,
         Color.White,
         0,
         Vector2.Zero,
         SpriteEffects.None,
         0);
     _spriteBatch.End();

     base.Draw(gameTime);

【讨论】:

  • 嗯,我尝试在 .Draw 方法的开头和结尾添加 .Begin 和 .End 。它不会导致任何错误。但是我加载的图像除了由 GraphicsDevice.Clear 生成的纯色之外什么都没有显示。造成这种情况的可能问题是什么?那是因为我的“坏” xnb 文件还是有其他可能性?谢谢
  • 这是我用作 .xnb imageshack.us/scaled/large/15/loadingo.png 的 .png 文件(它是纯黄色,仅用于加载图像测试目的)我使用模拟器 720p 是否有任何兼容性问题或问题我的头像?
  • 您是简单地将 .png 重命名为 .xnb 还是使用内容库将其转换为 .xnb?如果您只是简单地重命名它,那么您应该将其保留为 .png 并将该扩展名添加到您的调用中 _bg = Content.Load(@"Loading.png");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多