【发布时间】:2012-01-14 06:05:10
【问题描述】:
我成功生成了我的瓦片地图,它可以工作,但是当我想滚动它时,瓦片的顶行正在移动,所有瓦片都被拖到屏幕上。这很难解释,
想象一下这是瓷砖:X
XXXXXXXXXXXXXXXXXXXXXXXXXXX //这里开始在相机移动时逐块移除。
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX //使所有其他瓷砖都向上移动我的线。
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
看看我的代码:
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。按住键盘上的左键时它会递减。
这是我从数组中绘制的瓦片图(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,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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};
有什么想法吗?
【问题讨论】:
-
我强烈建议您将网格存储为二维数组,并使用 2(嵌套)循环。它会让代码更清晰,更容易理解。
标签: c# xna side-scroller