【发布时间】: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 上的精灵正常设置碰撞?
-
我会将其作为答案,以便您接受。我认为碰撞会保持不变,你测试了吗?