简体   繁体   中英

2D Depth System Method XNA C#

I am creating a 2D Top-Down RPG game like the Legend of Zelda: A link to the past.

when the player is drawn i want to update the tree depth so that it is over and under the player when the player Y position is higher or lower than the trees Y position, i think whether the tree is on top or not should be decided by the players Y position, being lower or higher. is it possible to do this in this way? can i change the layer depth in the update method?

I'm not sure you can. i'm not very experienced when coding. If there's a right way of doing this even without useing the spriteBatch layer depth i would appreciate any help.

I have discovered how to use the layer depth in the

spriteBatch.Draw();

Method.

What i am attempting to do is shown in the images below

树前的女人

树后的女人

The code i am using to separate trees from grass tiles uses layer depth like below:

spriteBatch.Begin(SpriteSortMode.FrontToBack, blendState = null, samplerState = null, DepthStencilState.DepthRead);

spriteBatch.Draw(tree, treePos, null, null, Vector2.Zero, 0, null, Color.White, SpriteEffects.None, 0.6f);

spriteBatch.End();

Tile draw method for the

tile

has the float value set

0.5f

绘制带有值和意义的图像线

Here's a way to do it:

Slice your sprites and do tile-based rendering, starting from the ground up to the sky.

Say you have a tile grid whose each is 32*32 pixels, if you have a tree that is 32*96 pixels, then when sliced it will be like:

A // layer 2
A // layer 1
A // layer 0 (floor)

Logic:

  • draw all tiles on floor 0
  • draw all tiles on floor 1
  • etc ...

Then obviously the part that should hide your player will be drawn correctly (before, if player is behind).

(note that the lowest tile of the tree should not be walk-able)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM