0

So far, this is my first XNA game and I'm having real trouble trying to learn this. I'm following a tutorial from Microsoft, found here: XNA Xbox Live Indie Games

Every now and again, the code breaks. Admittedly, I have taken a couple of bits out that I didn't think I'd need and I've created two enemy classes rather than just the one, but I don't think I hit any major faultlines with my adjustments.

In the Draw() method in the main Game1.cs file, I've had to include a for loop that will iterate through a list of available enemies and draw them upon update. However, the line of code flags up as incorrect, and I have absolutely no idea why. I followed the tutorial, and it looks like it should work, but it doesn't. Here's the entire Draw() method:

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

        backRect.Width = 800;
        backRect.Height = 480;

        // TODO: Add your drawing code here
        // Start drawing
        spriteBatch.Begin();

        spriteBatch.Draw(backgroundTexture, backRect, Color.White);

        // Draw the Player
        player.Draw(spriteBatch);

        for (int i = 0; i < goblins.Count; i++)
        {
            goblins[i].Draw(spriteBatch);
        }


        // Stop drawing
        spriteBatch.End();

        base.Draw(gameTime);
    }

It's the code inside the for loop that won't work. Any ideas how to fix it and/or any suggestions for a better tutorial?

Tom Sykes
  • 161
  • 2
  • 14

2 Answers2

0

You always need to call SpriteBatch.Begin() and SpriteBatch.End() on your sprite-batches. I am not sure about mixing them, but try to avoid it and use as few spritebatches as possible.

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

    backRect.Width = 800;
    backRect.Height = 480;

    // TODO: Add your drawing code here
    // Start drawing
    spriteBatch.Begin();

    spriteBatch.Draw(backgroundTexture, backRect, Color.White);

    // Draw the Player
    spriteBatch.Draw(playerTexture, playerRect, Color.White);

    for (int i = 0; i < goblins.Count; i++)
    {
        spriteBatch.Draw(goblins[i].Texture, goblins[i].Rect, Color.White);
    }


    // Stop drawing
    spriteBatch.End();

    base.Draw(gameTime);
}

See here for the documentation.

pinckerman
  • 4,115
  • 6
  • 33
  • 42
bash.d
  • 13,029
  • 3
  • 29
  • 42
  • Thanks for the referral, the code seems to like that. There's no error anymore (I had to add a Rectangle variable into the Goblin class), but there's still no enemies appearing.. do you know of a good tutorial for XNA game making? This one just seems to be causing me trouble, I just don't know why. – Tom Sykes Apr 10 '13 at 19:23
  • [MSDN](http://msdn.microsoft.com/en-us/library/bb200104.aspx) offers much for you. – bash.d Apr 10 '13 at 19:25
0

I like this tutorial a lot: http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/getting_started

It got me started off pretty well.

Colton
  • 1,297
  • 14
  • 27