开发者

layerdepth in different Begin() and End() block of spritebatch

I have two different draw method, one of them Game1.Draw() the other one is DrawableGameComponent.Draw() When I try to draw string with layerdepth in draw method of DrawableGameComponent, it's not working, it's drawing to front of spritebatch, the layerdepth value 开发者_开发问答is not effecting the drawing.

So what's the problem, how can I give layerdepth effect?


Sprites are only sorted inside a single SpriteBatch Begin/End block, it cannot possibly sort sprites that were drawn in different begin/end blocks.

If you want to achieve a layered effect you'll need to make sure that all your drawing takes place inside a single begin/end block, or else switch to deferred soting and take much finer control over the order in which you draw things (painters algorithm)


For sorting to work with sprite batches you need to begin your spritebatch with one of the overloads that accepts a SpriteSortMode

Then set the SpriteSort mode to either BackToFront or FrontToBack, then draw using the spriteBatch. Draw overload that accepts a layerDepth which is a float that you can vary from 0.0~1.0 to give ordering to your sprites.

Alternatively you can use SpriteSortMode.Deferred, in that case the first thing you draw is drawn at the background, the second thing you draw is drawn over that, etc...

Note that when you are drawing partially transparent sprites the recommended sort mode is BackToFront.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜