1 Replies - 2622 Views - Last Post: 10 July 2016 - 11:39 AM

#1 jmaslaki  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 02-February 16

Draw textures in certain order(layer depth?)

Posted 10 July 2016 - 03:30 AM

I'm trying to draw textures in a certain order according to their Position.Y

For example:

if (kiwi.Position.Y < orange.Position.Y){
//i want kiwi to be behind orange
// and orange to be in front
}


After setting the SpriteSortMode to BackToFront, I was told on another community to pass in the "Position.Y" for the layer depth parameter in spriteBatch.Draw(). But they were too vague for my understanding, so I was at a dead end. They ignored me when I asked them to just show me how to implement that.

I tried this:

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Scale);
        spriteBatch.Draw(p1, position, null, Color.White, 0f, Vector2.Zero, new Vector2(1, 1), SpriteEffects.None, position.Y);
        spriteBatch.Draw(p2, position2, null, Color.White, 0f, Vector2.Zero, new Vector2(1, 1), SpriteEffects.None, position2.Y);
        spriteBatch.End();


Now, the textures only appear if their Position.Y is at 0. I don't know what to do. Can someone please assist me? Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Draw textures in certain order(layer depth?)

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Draw textures in certain order(layer depth?)

Posted 10 July 2016 - 11:39 AM

There is an overload of SpriteBatch.Draw that takes as a parameter layerDepth, see below. This is a value between 0.0f and 1.0f where 1.0f is the top layer and 0.0f is the bottom layer. You can now calculate which order to draw your items in. If you want kiwi above orange you could call draw for kiwi with .75f and orange with .25f, just as an example. They will need to be drawn in the same batch for this to be effective though.

spriteBatch.Draw(texture, position, sourceRectangle, color, rotation, orign, scale, effects, layerDepth);



*edit*
Reference link:
https://msdn.microso...y/ff433989.aspx

This post has been edited by SixOfEleven: 10 July 2016 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1