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)

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

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


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.Draw(backgroundTexture, position,Color.White);
                        spriteBatch.Draw(draggingTexture, position,Color.White);

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.

| improve this answer | |
  • \$\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\$ – user2211593 Nov 22 '13 at 15:41
  • \$\begingroup\$ SpriteBatch itself has depth support, so there's no need to implement this yourself. \$\endgroup\$ – me-- Nov 27 '13 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.