0
votes

I know that I can change the order drawing sprite (which is in front of the other) by ordering the lines, but how to change that order for example when a button is pressed?

How can I make something like in this game This is a 2d game which the order of sprites change according to its position

1
You could use .OrderBy() on the collection of sprites perhaps. - Magus
I remember something like drawComponent or something like that, I couldnt understand how I can use it to make order change according to position , I would like to know how or if there is another way . - Mohamed Atef
Frankly, unless you show us where you're having problems in your code, you are unlikely to get an answer, and could get your question closed. - Magus

1 Answers

2
votes

You need to use a different SpriteSortMode than Deferred (the default). SpriteSortMode can be set when you use a SpriteBatch.Begin() with a SpriteSortMode parameter.

With Deferred (the default) the order in which SpriteBatch.Draw() is called determines what overlaps what (what you call ordering the lines). If you use SpriteSortMode.FrontToBack or SpriteSortMode.BackToFront you can control the order they're drawn by the layerDepth parameter of SpriteBatch.Draw().

So what you'd do is you'd keep track of the value that responds to input and pass it to SpriteBatch.Draw(). For example you have a variable called float mySpriteZindex and if a certain button is pressed you increase or decrease mySpriteZindex. Then you can pass that value to SpriteBatch.Draw(), and now you have a way to control the order sprites are drawn in by variables.