Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在平铺贴图XNA上显示精灵_C#_Xna - Fatal编程技术网

C# 在平铺贴图XNA上显示精灵

C# 在平铺贴图XNA上显示精灵,c#,xna,C#,Xna,我正在学习XNA,但在将精灵放置到瓷砖贴图上时遇到了一些困难 以下是我尝试过的: 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 m

我正在学习XNA,但在将精灵放置到瓷砖贴图上时遇到了一些困难

以下是我尝试过的:

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)后面。如何使其在平铺贴图的顶部而不是下方移动


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


进一步阅读:

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