c# - 侧滚动瓦片 map , map 变得困惑

标签 c# xna side-scroller

我成功生成了我的瓦片 map 并且它可以工作,但是当我想滚动它时,瓦片的顶行正在移动并且所有瓦片都被拖出屏幕..这很难解释,

想象这是瓷砖:X

XXXXXXXXXXXXXXXXXXXXXXXXXX//这里是在相机移动的时候开始逐 block 移除

XXXXXXXXXXXXXXXXXXXXXXXXXX//导致所有其他瓷砖向上移动我的线。

XXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXX

看看我的代码:

public void Draw(SpriteBatch spriteBatch)
{
    currentLevel = level1;
    //Its here i need the value from Player class!
    int currentX = cameraX; //The current x cordiate to draw the tile too
    int currentY = 0; //The current y cordiate to draw the tile too
    for (int i = 0; i < currentLevel.Length; i++)
    {
        /*
         * A switch statement to see if the current level1[i] is either a grass, sky or stone tile
         */
        switch(currentLevel[i])
        {
            case 1:
                //Draw a sky tile
                spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), skyTile, Color.White);
                break;

            case 2:
                //Draw a grass tile
                spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), grassTile, Color.White);
                break;

            case 3:
                //Draw a stone tile
                spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), stoneTile, Color.White);
                break;
        }

        //Add 32 to the current cordinate so we don't draw to the same spot all the time
        currentX = currentX + 32; 
        if (currentX >= 896) //If we hit the width of the map we want:
        {
            //Start drawing from X cordinate zero
            cameraX = 0;
            currentX = cameraX;
            //And Y cordinate + 32
            currentY = currentY + 32;
            //This will make so we draw one line and when we hit a full line on the map then we draw next line and soo on.
        }
    }
}

CameraX 变量是来自 Player 类的 getter。按住键盘上的左键时它会递减。

这是我从数组中绘制的瓦片 map (1 = 天空,2 = 草地,3 = 石头:

int[] level1 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
                         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3};

有什么想法吗?

最佳答案

看起来您总是在绘制整个 map 中的所有图 block (我从 0 开始并遍历每个图 block )。但是,您并不总是在每行上绘制相同数量的图 block 。如果 CameraX 为 0,则每行将绘制 28 个图 block ,因为您将在 32*28 (896) 像素后移动到下一行。但是如果 CameraX 是 480,那么你只会在每行上绘制 13 ((896-480)/32) 个图 block ,第一行的其余图 block 将在第二行上绘制(因为你仍在尝试绘制 所有瓷砖)。我认为您应该遍历要绘制的每一行和每一列,并从中计算图 block 索引,而不是遍历 map 中的每个图 block 并独立计算位置。

编辑 下面是一些示例代码,用于在给定列和行索引的情况下计算一维数组中的图 block 索引,假设您知道列数。 (或者您可以改用二维数组):

i = row * columnCount + column;

关于c# - 侧滚动瓦片 map , map 变得困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8403275/

相关文章:

c# - 如何在通过 Visual Studio 发布的 C# 程序中包含未编译的内容

java - 使用for循环在地面上绘制方 block

c# - 为什么 DragLeave 事件不使用 DragEventHandler?

c# - 获取特定节点的 xml 属性值

c# - XNA 瞄准示例中的 StackOverflowException

c# - XNA/MonoGame的音效和音乐减慢了我的游戏速度

swift - 连续触摸/移动 Swift & SpriteKit

javascript - html5 中的 2D 侧滚动相机 View

c# - 什么是将 IDictionary<string, object> 转换为 IDictionary<string, string> 的优雅方法

c# - 使用 xval 客户端验证表单