Я успешно сгенерировал свою карту тайлов, и она работает, но когда я хочу прокрутить ее, верхняя линия тайлов перемещается, и все тайлы перетаскиваются на экран. Трудно объяснить,
Представьте, что это плитки: X
XXXXXXXXXXXXXXXXXXXXXXXXXX // Здесь начинается удаление плитки за плиткой, когда камера движется.
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. Уменьшается при удерживании левой кнопки на клавиатуре.
Вот карта тайлов, которую я рисую из массива (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, 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