Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:48:41+00:00 2026-06-13T19:48:41+00:00

I have just started learning c# in xna and I’m making a simple tower

  • 0

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

        }




    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:48:42+00:00Added an answer on June 13, 2026 at 7:48 pm

    I have found what was wrong in the code. When I added the line Game1 game = new Game1(); in my method DrawMap() 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 line map = new Map1(); in the initialize method it worked perfectly. Thank you for the help I’ve gotten here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have just started learning WCF and I have encountered an error I
I have just started learning GWT. My question is pretty simple: If I create
I have just started learning XNA. This is my first program that I am
I have just started learning Qt and I am trying to create a simple
I have just started learning wordpress plugin development and got this error when I
I have just started learning python and i was wondering how i would get
I have just started learning OpenGL and I am attempting to make a game
I have just started learning about Android and I was planning on making a
I have just started learning Android and thought of a simple project to help
I have just started learning python version 3 and trying to create a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.