编程语言
首页 > 编程语言> > C#-在图块地图XNA上显示子画面

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 =途径

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

谢谢

编辑:我遇到的问题是,如果将sprite(右下角的黑色正方形)向左移动,则移到棕色(是平铺图的prt)之后.我如何做到使其移动到地图的顶部而不是下方

解决方法:

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

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

标签:xna,c
来源: https://codeday.me/bug/20191201/2079571.html