1
\$\begingroup\$

I am making a simple board game. In the game there is a fixed background called myTexture and others are textureGoat and textureTiger whicha are to be placed on top of the background(myTexture). But i am having problem that fourth and fifth component is not displaying however, the sixth component( i.e. myTexture) is appearing. Here is my code, please look at it

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Green);

        // TODO: Add your drawing code here
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

        //placing tiger
        spriteBatch.Draw(textureTiger, new Rectangle(22, 25, 50, 50), Color.White);//first component
        spriteBatch.Draw(textureTiger, new Rectangle(22, 407, 50, 50), Color.White);//second component


        spriteBatch.Draw(textureTiger, new Rectangle(422, 25, 50, 50), Color.White);//third component
        spriteBatch.Draw(textureTiger, new Rectangle(422, 407, 50, 50), Color.White);//fourth component


        //placing goat
        spriteBatch.Draw(textureGoat, new Rectangle(125, 110, 50, 50), Color.White);//fifth component

        //placing background
        spriteBatch.Draw(myTexture, new Rectangle(0, 0, 500, 500), Color.White);//sixth component

        spriteBatch.End();
        base.Draw(gameTime);
    }
\$\endgroup\$
6
  • \$\begingroup\$ is the third showing? \$\endgroup\$
    – Valmond
    Oct 3, 2012 at 7:45
  • 3
    \$\begingroup\$ The 6th texture is drawn over the entire region and on top of your other textures. Does this texture contain anything in its lower right part, where you are rendering your 4th texture? Also, please include a screenshot so we can get a better clue of what it looks like. \$\endgroup\$
    – Dadgron
    Oct 3, 2012 at 7:57
  • \$\begingroup\$ I'm agreeing with Aaron here, your board texture is drawn last (see en.wikipedia.org/wiki/Painter's_algorithm) so it covers the other images. If you're seeing some of the Goats/Tigers I'm guessing your board has some alpha somewere? Also, you shouldn't need to pass any parameters to spriteBatch.Begin(); \$\endgroup\$ Oct 3, 2012 at 8:17
  • \$\begingroup\$ all: sorry for delay in replying... @Valmond:yes third is showing \$\endgroup\$ Oct 3, 2012 at 8:47
  • \$\begingroup\$ @Aaron: yes it contains the fourth element.. u can check by coordinates.. rendering textureTiger as shown in code \$\endgroup\$ Oct 3, 2012 at 8:50

2 Answers 2

0
\$\begingroup\$

you are drawing background last, thus covering everything drawn before.

when you use immediate sorting (i.e. you don't explicitly specify the z-order), you need to draw from bottom to top, as if you were layering transparent paper, where each Draw call is a new layer. you draw background first, then what is supposed to be on background second, etc..., so if your comments mean that "first component" is supposed to be topmost, you need to completely reverse the order of your draw calls.

\$\endgroup\$
2
\$\begingroup\$

Since you are using BackToFront sort mode you might want to use this draw method;

SpriteBatch.Draw (Texture2D, Vector2, Nullable, Color, Single, Vector2, Vector2, SpriteEffects, Single)

where the last element is the "Sort Depth". This is the parameter that will be used by your sprite batch to choose the order in which to draw things. Since the draw method you are using doesn't set the "Sort Depth" value all sprites will be drawn in the same order you called draw in.

As an alternative, you can simply change the order in which you are calling Draw for each sprite, with objects at the back being drawn first so they don't get drawn over the top.

NOTE:

BackToFront is the sort order recommended for drawing sprites which have some transparency. If your textures have no transparent areas then you will want to use FrontToBack. Also, if you have some with no transparency and some with transparency, you might consider drawing the non-transparent ones first with a sort mode set to FrontToBack and then draw the transparent ones with the sort mode set to BackToFront. Unless performance becomes an issue, I would stick with BackToFront for all sprites and set the "Sort Depth". You can always change it later.

\$\endgroup\$

You must log in to answer this question.

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