0
\$\begingroup\$

I am working on puzzle game where tiles are needed to be dragged to their correct place, but at the time of dragging one of the textures goes behind the other texture. How can I bring the texture to the top?

       private void OnDraw(object sender, GameTimerEventArgs e)
       {
                         SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

         // TODO: Add your drawing code here
        Color transp = Color.White;
     //   transp.A = 200;
        spriteBatch.Begin();       

                   spriteBatch.Draw(Texture.piece[1],Texture.piecerect[1],null, transp,0,Vector2.Zero,SpriteEffects.None,1);


        spriteBatch.End();
    }
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you use deafult spriteBatch.Begin(), then textures draw in order you call them. This mean you must simply draw dragging texture after background texture

                        spriteBatch.Begin();
                        spriteBatch.Draw(backgroundTexture, position,Color.White);
                        spriteBatch.Draw(draggingTexture, position,Color.White);
                        spriteBatch.End();

Another approach that i can suggest is to implement draw layers. One way to do that is to create an array from 0 to 5 for example and let 0 be top layer. Then assign to each texture layer number. Change texture layer in Update as you want. In Draw you then just simply iterate from 5 to 0 layer and draw coresponding to this layer textures.

\$\endgroup\$
2
  • \$\begingroup\$ it increased my code line's too much...because of puzzle tiles are randomly generated so its quite tricky to handle their sequence... but solved the problem so easily ..just becasue of your idea \$\endgroup\$ Nov 22, 2013 at 15:41
  • \$\begingroup\$ SpriteBatch itself has depth support, so there's no need to implement this yourself. \$\endgroup\$
    – me--
    Nov 27, 2013 at 22:31

You must log in to answer this question.

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