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

  • SEARCH
  • Home
  • 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 6899853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:28:26+00:00 2026-05-27T07:28:26+00:00

I’m trying to draw a tile map in C# and the problem I’m having

  • 0

I’m trying to draw a tile map in C# and the problem I’m having is strange in my opinion.

I have this int array that is suposed to hold the x coordinate and y coordinate to draw the tiles on the screen. (The arrow with not only 0’s in it is X and other one is Y)

int[,] level1 = { { 0, 32, 64, 96 }, { 0, 0, 0, 0 } };

Here is how I use for loops to render a part of the tile into the screen and it’s here I’m getting an “OutOfMemoryException” on a line that I will comment out:

public void DrawTest(SpriteBatch spriteBatch)
    {
        for (int x = 0;; x++)
        {
            for (int y = 0;; y++)
            {
                x = level1[x, 0];
                y = level1[0, y];

                //This line bellow is where it says OutOfMemoryException
                spriteBatch.Draw(tileSheet, new Rectangle(x, y, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);

                if (x >= 5 | y >= 5)
                {
                    x = 0;
                    y = 0;
                }
            }
        }
    }

When i want to call this render method i do it in the main class Render method

levelLoader.DrawTest(this.spriteBatch);

It worked perfectly before i used this DrawTest method to try drawing the tiles. But I have completely no idea why this isn’t working correctly.

UPDATE:

public void DrawTest(SpriteBatch spriteBatch)
        {
            for (int x = 0; x < 5 ; x++)
            {
                for (int y = 0; y < 5 ; y++)
                {
                    x = level1[x, 0];
                    y = level1[0, y];
                    spriteBatch.Draw(tileSheet, new Rectangle(x, y, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
                }
            }
        }

UPDATE2:

        public void DrawTest(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < 5 ; x++)
        {
            for (int y = 0; y < 5 ; y++)
            {
                int tileXCord = level1[x, 0];
                int tileYCord = level1[0, y];
                spriteBatch.Draw(tileSheet, new Rectangle(tileXCord, tileYCord, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
            }
        }
    }
  • 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-05-27T07:28:26+00:00Added an answer on May 27, 2026 at 7:28 am

    I see several problems in your code:

    1. You have an infinite loop here in your code. What is the termination condition for your nested loops?
    2. (important!) The spriteBatch.Draw() method doesn’t draw anything, it just schedules the drawing of your sprites. This method invocation should be preceded by spriteBatch.Begin() (to start scheduling he drawing) and eventually you must call spriteBatch.End() to flush the scheduled spites to your device. You infinite loops cause the infinite scheduling of your sprite drawing until the memory is full and you are facing the out of memory exception.
    3. (note!) In you condition (x >= 5 | y >= 5) you are using a bitwise OR comparison, you shouldn’t do it (unless on purpose, which I don’t see here and rather use boolean OR: (x >= 5 || y >= 5)
    4. It is a very bad habit to modify loop counters within the loop itself. Not it is only errors prone, it is also difficult to understand and support your code written this way.

    I would re-write your code this way

        spriteBatch.Begin();
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                x = level1[x, 0];
                y = level1[0, y];
    
                //This line bellow is where it says OutOfMemoryException
                spriteBatch.Draw(tileSheet, new Rectangle(x, y, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
            }
        }
        spriteBatch.End();
    

    It will re-draw all your tiles on every Draw() event of the main gameloop (provided you are still calling this method from your Draw() method of your Game class. XNA varies the FPS rate depending upon the performance of you PC and the amount of calculation it has to be done on every frame.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

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.