I have just started learning c# in xna and I’m making a simple tower defense game. I get an error message when I add the line.
graphic.ApplyChanges();
Here is a link to a screen shot of the error message.
When I start the game it runs for a few seconds and then it closes and shows me this message.
I have made a few other games before this one and I have never had this problem before. I’m a beginner to programming so let me know if there is something else you need to know to help me.
Update:
I have tried to reproduce the error in a new project, but without success. I’m going to post my code here so someone with more knowledge than me can read it and maybe find what causes the error.
The Game1 class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
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;
namespace AdventureGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D tilegraphic;
MouseState oldmouse;
MouseState currentmouse;
Point mouseposition;
Color defaultcolor;
Color selectedcolor;
Color pathcolor;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
IsMouseVisible = true;
// TODO: Add your initialization logic here
base.Initialize();
oldmouse = new MouseState();
currentmouse = new MouseState();
defaultcolor = new Color();
selectedcolor = new Color();
defaultcolor = Color.LawnGreen;
selectedcolor = Color.DarkGreen;
pathcolor = Color.SaddleBrown;
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
tilegraphic = Content.Load<Texture2D>("tilegraphic") as Texture2D;
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
oldmouse = currentmouse;
currentmouse = Mouse.GetState();
mouseposition = new Point(currentmouse.X, currentmouse.Y);
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
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)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
DrawMap();
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
public bool MouseJustPressed()
{
if (oldmouse.LeftButton == ButtonState.Released && currentmouse.LeftButton == ButtonState.Pressed)
{
return true;
}
else
{
return false;
}
}
public void DrawMap()
{
Map1 map = new Map1();
map.Layout();
map.CreateRectangles();
for (int x = 0; x < map.mapwidth; x++)
{
for (int y = 0; y < map.mapheight; y++)
{
switch (map.tiles[x, y])
{
case 0:
if (map.tilerectangles[x, y].Contains(mouseposition))
{
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], selectedcolor);
}
else
{
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], defaultcolor);
}
break;
case 1:
spriteBatch.Draw(tilegraphic, map.tilerectangles[x, y], pathcolor);
break;
}
}
}
}
}
}
The Map1 class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
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;
namespace AdventureGame
{
class Map1
{
public int[,] tiles = new int[7, 7];
public Rectangle[,] tilerectangles = new Rectangle[8, 8];
Game1 game = new Game1();
public int mapwidth = 8;
public int mapheight = 8;
public void Layout()
{
tiles = new int[,] {
{0,1,0,0,0,0,0,0},
{0,1,0,1,1,1,1,1},
{0,1,0,1,0,0,0,0},
{0,1,0,1,1,1,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0},};
}
public void CreateRectangles()
{
for (int x = 0; x < mapwidth; x++)
{
for (int y = 0; y < mapheight; y++)
{
tilerectangles[x, y] = new Rectangle(x * 70, y * 70, 70, 70);
}
}
}
}
}
I have found what was wrong in the code. When I added the line
Game1 game = new Game1();in my methodDrawMap()for a quick test to see if it worked so far the program apparently didn’t like that and gave me an error. So when I declared it with the rest of my variables and put the linemap = new Map1();in the initialize method it worked perfectly. Thank you for the help I’ve gotten here.