【问题标题】:displaying a sprite on a tile map XNA在平铺地图 XNA 上显示精灵
【发布时间】:2012-05-29 19:34:45
【问题描述】:

我正在学习 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)后面。我如何让它在瓦片地图的顶部而不是在下面移动

【问题讨论】:

  • 为 Nintendo DS 做了类似的事情。使用自定义字体文件但用精灵替换字符,因此X 将是一堵墙,Z 将是一个敌人。您需要循环绘制您的精灵的数组,其中i = X 等...
  • @duane 嗨,请检查我的编辑
  • 您可以通过在绘图前设置SpriteSortMode 来阻止这种情况发生
  • 谢谢杜安!!!!但现在我不能接受答案。想知道我是否仍会为 tilemap 上的精灵正常设置碰撞?
  • 我会将其作为答案,以便您接受。我认为碰撞会保持不变,你测试了吗?

标签: c# xna


【解决方案1】:

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

延伸阅读:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritesortmode.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多