1

So this is my Game1 class :

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);
         }
     }
 }

I make a "Content" folder in my project, and add Loading.xnb as existing item. And then, I change the Build Action of the Loading.xnb to "Content" and the Copy to Output as "Copy Always".

But when I compile it, this section throw 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);
     }

Specifically at the _spriteBatch.Draw(.......) method. Can somebody help me? Thanks.

Moses Aprico
  • 1,951
  • 5
  • 30
  • 53

1 Answers1

1

Look at some of the example programs. _spriteBatch.Draw must be called between _spriteBatch.Begin and _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);
ClassicThunder
  • 1,896
  • 16
  • 25
  • hmm, i've tried to add the .Begin and .End at the start and end of the .Draw method. And it result in no error. But The image I loaded doesn't show anything but the plain color produced by the GraphicsDevice.Clear. What's the possible problem causing this?? Is that because my "bad" xnb files or are there any other possibility? Thanks – Moses Aprico Apr 26 '13 at 15:17
  • This is my .png file that I use as .xnb https://imageshack.us/scaled/large/15/loadingo.png (It is a plain yellow, just for load image testing purpose) I use emulator 720p is there any compatibiliy issue or something problematic with my image? – Moses Aprico Apr 26 '13 at 15:40
  • Are you simply renaming the .png to .xnb or are you converting it to .xnb using a content library? If you are simply renaming it then you should keep it as .png and add that extension to your call _bg = Content.Load(@"Loading.png"); – RobCurr Mar 14 '14 at 17:05