在瓷砖地图XNA上显示精灵

时间:2012-05-29 11:05:15

标签: c# xna

我正在学习XNA,但是将精灵放在tilemap上有一些麻烦。

以下是我的尝试:

public void Draw(SpriteBatch spriteBatch,int tileWidth,int tileHeight)
    {
        spriteBatch.Draw( texture , position , Color.White );
    }// This code draws the sprite but the sprite is not on the tile map but outside it.


public void Draw(SpriteBatch spriteBatch,int tileWidth,int tileHeight)
    {
        spriteBatch.Draw( texture , new Rectangle( ( int )position.X * tileWidth , ( int )position.Y * tileHeight , tileWidth , tileHeight ) , Color.White );
    }// And this code does nothing, doesnt even draw the sprite

这是二维数组:

int[ , ] map = {
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                        {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                   };

其中0 =墙壁&       1 =途径

请告诉我或告诉我如何仅在地图上绘制精灵?

由于

编辑:我遇到的问题是精灵(右下角的黑色正方形)如果向左移动,则会落在棕色背后(即瓷砖地图的prt)。我如何使它在瓷砖地图上移动而不是在

下面移动

In game picture

1 个答案:

答案 0 :(得分:3)

为防止您的精灵落在棕色瓷砖后面,您需要在调用SpriteSortMode之前设置SpriteBatch.Begin()属性

进一步阅读:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritesortmode.aspx