2
\$\begingroup\$

I'm struggling with getting the depth layer to work properly. No matter what I do, the line is drawn over the top of the circle texture.

https://i.sstatic.net/DVPrO.png

public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin(
            SpriteSortMode.BackToFront, 
            BlendState.AlphaBlend, 
            null, 
            null, 
            null,
            null, 
            Camera.getTranslation());

        // Draw all planets
        foreach (PlanetNode p in planets)
        {
            p.Draw(spriteBatch);
        }

        spriteBatch.End();
    }

PlanetNode's Draw:

public void Draw(SpriteBatch spriteBatch)
{
    // Draw forwarding line
    if (resourceForward)
    {
        double dist = Math.Sqrt(Math.Pow(location.X - forwardTo.location.X, 2) +
                                Math.Pow(location.Y - forwardTo.location.Y, 2));
        float angle = Utility.GetAngle(location, forwardTo.location);
        spriteBatch.Draw(Textures.BlackPixel, new Rectangle((int)location.X, (int)location.Y, (int)dist, 1), null, Color.Black, angle, Vector2.Zero, SpriteEffects.None, 0.9f);
    }

    // Draw Planet
    if (mouseOver)
    {
        Color c = new Color(color.R + 0.2f, color.G + 0.2f, color.B + 0.2f);
        spriteBatch.Draw(Textures.Planet, position, null, c, 0f, texOrigin, SpriteEffects.None, 0.5f);
    }
    else
        spriteBatch.Draw(Textures.Planet, position, null, color, 0f, texOrigin, SpriteEffects.None, 0.5f);

    // Draw overlay text
    Vector2 size = Textures.TextFont.MeasureString(resources.ToString());
    spriteBatch.DrawString(Textures.TextFont, resources.ToString(), new Vector2(position.X - size.X / 2, position.Y - size.Y / 2), Color.Black);
}
\$\endgroup\$
1
  • \$\begingroup\$ Only thing I can spot is are you missing GraphicsDevice.Clear(Color.CornflowerBlue); at the start of your Draw(). Otherwise your sort mode and layer values look fine to me. With sort mode set to BackToFront the smaller layer value should be on top which is what you seem to have done. \$\endgroup\$
    – TerryB
    Commented Feb 10, 2012 at 2:28

1 Answer 1

1
\$\begingroup\$

To be honest, judging by the way you're drawing your sprites (which all look fine to me, by the way) it may be better to just switch to SpriteSortMode.Deferred and keep a constant layerDepth.

Doing this and keeping your layerDepths at 0 will produce the result you want.

So, a quick sample:

 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
 //Draw your line here

 //Draw your Planet(s) here with layerDepth set to 0

 spriteBatch.End();

If, for some reason, you desperately need to use layerDepth, I will take another look and update my answer for you.

\$\endgroup\$
2
  • \$\begingroup\$ Ahhh it seems that the planets are sort of semi-transparent. Thank you all for the help though, but I'm pretty sure that its this transparency issue. Thats what I get for downloading a texture from the internet.. \$\endgroup\$
    – Ben
    Commented Feb 10, 2012 at 4:09
  • \$\begingroup\$ :) I was gonna suggest that but then I thought "nahhhh" \$\endgroup\$
    – TerryB
    Commented Feb 10, 2012 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .