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 7578183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:22:18+00:00 2026-05-30T17:22:18+00:00

I’m working on a tile/sprite-based game, calling my Draws via public void Draw(SpriteBatch spriteBatch)

  • 0

I’m working on a tile/sprite-based game, calling my Draws via

public void Draw(SpriteBatch spriteBatch)
    {
        DrawBehind(spriteBatch);
        DrawEven(spriteBatch);
        DrawInFront(spriteBatch);
    }

    private void DrawBehind(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        ActiveMap.DrawAtDepth(spriteBatch, Player, 1);
        spriteBatch.End();
    }

    private void DrawEven(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin(SpriteSortMode.BackToFront, null);
        ActiveMap.DrawAtDepth(spriteBatch, Player, -1);
        Player.Draw(spriteBatch);
        spriteBatch.End();
    }

    private void DrawInFront(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        ActiveMap.DrawAtDepth(spriteBatch, Player, 0);
        spriteBatch.End();
    }

As expected, DrawBehind and DrawInFront just draw based on the order they are called, stable and well on both X and Y.

DrawEven sorts on the Y-axis correctly, but on the X-axis, the sprites just seem to flip in front of eachother and back for no apparent reason.

If it would be stable, I don’t really care in which order it sorts it. Instead, everytime I “move my character” (move the position of all the sprites but my character’s), it flickers and switches all around. Anyone knows what causes this or how to fix it?

As per request

EDIT:

Map:

public void DrawAtDepth(SpriteBatch spriteBatch, Player player, int depth)
    {
        Area.DrawAtDepth(spriteBatch, player, depth);
    }

Area:

public void DrawAtDepth(SpriteBatch spriteBatch, Player player, int depth)
    {
        const int border = 10;
        var screenTiles = new Rectangle(
            0 - border,
            10 - border,
            (GameMain.ScreenRectangle.Width / Tile.Width - 1) + border,
            (GameMain.ScreenRectangle.Height / Tile.Height - 1) + 10 + border);

        var playerLevelPosition = player.LevelPosition;

        for (int layer = 0; layer < _layerCount; layer++)
            if (ActiveLevels[1, 1].GetLayerDepth(layer) == depth)
                for (int y = screenTiles.Y; y < screenTiles.Height; y++)
                    for (int x = screenTiles.X; x < screenTiles.Width; x++)
                        Draw(spriteBatch, player, playerLevelPosition, layer, x, y);
    }
private void Draw(SpriteBatch spriteBatch, Player player, Vector2 playerLevelPosition, int layer, int x, int y)
    {
        int levelX;
        var tileX = TilePos((int)playerLevelPosition.X, Level.Width, x, out levelX);

        int levelY;
        var tileY = TilePos((int)playerLevelPosition.Y, Level.Height, y, out levelY);

        var level = ActiveLevels[levelX, levelY];

        if (level != null)
            level.Draw(spriteBatch, player, layer, new Vector2(tileX, tileY));
    }

    private float TilePos(int playerPos, int mapBounds, int offset, out int levelPos)
    {
        var tilePos = ((playerPos) - (mapBounds/2f - 1)) + offset;

        levelPos = 1;
        if (tilePos < 0)
        {
            levelPos = 0;
            return tilePos + mapBounds;
        }
        if (tilePos >= mapBounds)
        {
            levelPos = 2;
            return tilePos - mapBounds;
        }
        return tilePos;
    }

    public static float GetDepth(float tilePos)
    {
        return 1 - tilePos / (Level.Height * 3);
    }

Layer:

public void Draw(SpriteBatch spriteBatch, Vector2 tile, Vector2 drawPosition, Color color, bool transparent = false)
    {
        if (_grid[(int)tile.X, (int)tile.Y] == null) return;
        _grid[(int)tile.X, (int)tile.Y].Draw(spriteBatch, drawPosition, color, transparent, (Depth != -1) ? Depth : Area.GetDepth(Level.Height + tile.Y));
    }

Tile:

public override void Draw(SpriteBatch spriteBatch, Vector2 drawPosition, Color color, bool transparent = false, float depth = 0)
    {
        if (!GameMain.ScreenRectangle.Intersects(_sprite.Bounds)) return;

        if (transparent && _fade)
            color = new Color(255, 255, 255, 100);

        spriteBatch.Draw(
            _sprite,
            drawPosition,
            null,
            color,
            0,
            new Vector2(_sprite.Width / 2 - Width / 2, _sprite.Height),
            1,
            SpriteEffects.None,
            depth
            );
    }

Also, any general tips for the sake of it are welcome. I’m a pretty new programmer, styling tips are appreciated.

  • 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-30T17:22:20+00:00Added an answer on May 30, 2026 at 5:22 pm

    I’m not completely familiar with the latest XNA 4, so this might not be -the- correct answer, but:

    From the symptoms you describe and the fact that it’s only happening when you set the BackToFront SpriteSortMode, I would expect that you’re either not setting a depth coordinate for some of the sprites being drawn in DrawEven or not making them different enough. As such, I believe that it is trying to override the normal deferred render order, but doesn’t have enough depth information and just picks an arbitrary order each frame (perhaps due to floating point inaccuracy? Not certain, but it seems likely.)

    Since you mention it’s sorting properly on the Y axis, then assuming you’re using the Y coordinate as an input to the layer depth, try adding a very small amount of the X coordinate as well. Assuming 32 by 32 tiles on screen, I would calculate the layer depth floating point value like this:

    float depth = (someSprite.ScreenYCoord / 32) + (someSprite.ScreenXCoord / 1025);
    spriteBatch.Draw(texture, position, sourceRect, color, rotation, origin, scale, spriteEffects, depth);
    

    where 1025 is 32 * 32 + 1 (to make sure that a difference of 1 tile in Y is always more important when sorting than even a full 32 tiles difference in X) and assuming a zero-based screen coordinate system where something can be on tile 0 to 31 in either axis.

    Also as far as I can tell ActiveMap.DrawAtDepth is not an XNA library function. Can you show us the code for that please, or show us wherever else the actual calls to SpriteBatch.Draw() are – that would help people work out what exactly is happening, and hopefully mean we have to guess less.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.