이것은 내 타일 맵 코드입니다.XNA에서 타일 맵으로 충돌 감지
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game_AI
{
class MapRow
{
public List<MapCell> Columns = new List<MapCell>();
}
class TileMap
{
public List<MapRow> Rows = new List<MapRow>();
public int MapWidth = 20;
public int MapHeight = 16;
public TileMap()
{
int[,] tiles = new int[,]{
{ 6, 0, 4,22, 8, 8, 8, 8, 8, 8,11, 8, 8, 8, 8,11, 8, 8, 8,21},
{ 6, 0, 4, 6, 0, 0, 0, 0, 0, 0,26, 0, 0, 0, 0,26, 0, 0, 0, 4},
{ 6, 0, 7, 9, 0,15,16,16,17, 0,26, 0, 1, 3, 0,26, 0,25, 0, 4},
{ 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,27, 0, 7, 9, 0,27, 0,26, 0, 4},
{24, 2, 3, 0, 1, 3, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0,26, 0, 4},
{22, 8, 9, 0, 7, 9, 0, 7, 9, 0, 1, 2, 3, 0, 1, 3, 0,27, 0, 4},
{ 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 4, 6, 0, 0, 0, 4},
{ 6, 0,25, 0, 1, 3, 0, 1, 3, 0, 7, 8, 9, 0, 7, 9, 0, 1, 2,23},
{ 6, 0,26, 0, 7, 9, 0, 7, 9, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8,21},
{ 6, 0,27, 0, 0, 0, 0, 0, 0, 0,25, 0, 1, 3, 0,25, 0, 0, 0, 4},
{ 6, 0, 0, 0, 0, 1, 2, 2, 3, 0,26, 0, 7, 9, 0,19,18,17, 0, 4},
{14,16,16,17, 0, 7,21,22, 9, 0,26, 0, 0, 0, 0, 4, 6, 0, 0, 4},
{ 6, 0, 0, 0, 0, 0, 4, 6, 0, 0,26, 0, 1, 3, 0, 4, 6, 0, 1,23},
{ 6, 0,15,16,17, 0, 4, 6, 0,15,20, 0, 4, 6, 0, 7, 9, 0, 7, 8},
{ 6, 0, 0, 0, 0, 0, 4, 6, 0, 0, 0, 0, 4, 6, 0, 0, 0, 0, 0, 0},
{24, 2, 2, 2, 2, 2,23,24, 2, 2, 2, 2,23,24, 2, 2, 2, 2, 2, 2}
};
for (int y = 0; y < MapHeight; y++)
{
MapRow thisRow = new MapRow();
for (int x = 0; x < MapWidth; x++)
{
thisRow.Columns.Add(new MapCell(0,true));
}
Rows.Add(thisRow);
}
for (int y = 0; y < MapHeight; y++)
{
for (int x = 0; x < MapWidth; x++)
{
Rows[y].Columns[x].TileID = tiles[y, x];
}
}
for (int y = 0; y < MapHeight; y++)
{
for (int x = 0; x < MapWidth; x++)
{
if (Rows[y].Columns[x].TileID != 0)
Rows[y].Columns[x].passable = false;
}
}
}
}
}
그래서 모든 타일을 색인 및 부울 값으로 설정할 수 있습니다. 이것은 내 스프라이트를 움직이는 방법이지만 항상 오류가 발생했습니다.
private Vector2 cekPosisiPlayer()
{
float tileX = pstChar.X/60;
float tileY = pstChar.Y/60;
return new Vector2(tileX, tileY);
}
private void cekTapTap()
{
// is there gestures available
while (TouchPanel.IsGestureAvailable)
{
// read gesture
GestureSample gesture = TouchPanel.ReadGesture();
// is it Flick or Tap
switch (gesture.GestureType)
{
case GestureType.Tap:
//posisiTap=gesture.Position;
posisiX = (int)gesture.Position.X;
posisiY = (int)gesture.Position.Y;
for(int y=0 ;y<squaresDown ;y++)
{
for(int x=0 ;x<squaresAcross ;x++)
{
//kanan
else if (posisiX > 120 && posisiX < 170 && posisiY > 360 && posisiY < 410)
{
kanan = true;
if (myMap.Rows[(int)cekPosisiPlayer().Y + 60].Columns[(int)cekPosisiPlayer().X + 60].passable)
{
pstChar.X += 5;
if (CharBawahX < 3)
{
CharBawahX++;
}
else if (CharBawahX >= 3)
{
CharBawahX = 0;
}
}
else
{
pstChar.X += 0;
}
//cameraX++;
atas = false;
bawah = false;
kiri = false;
kananchar = true;
kirichar = false;
ataschar = false;
bawahchar = false;
}
break;
//case GestureType.Hold :
}
}
}
잠시 동안 올바른 동작 만 확인하고 오른쪽으로 내 문자를 이동하면 충돌이 발생합니다. 뭔가 잘못 됐니? 또는 더 자세한 설명이나 코드가 필요하면 알려주십시오.
존재하지 않는 셀을 검사하기 때문에'NullReferenceException' 또는'IndexOutOfRangeException'이 발생합니다. 먼저 플레이어 위치를 확인하십시오. 행/열과 함께이 이상한 표현을 사용하는 이유는 무엇입니까? 2 차원 배열이 더 적합하고 유지하기가 더 쉬워 보입니다. –
타일 맵을 할 때 유용한 팁은'GetTile (x, y)'와 같은 메서드를 만드는 것입니다. 바운드를 벗어나면 오류 대신 빈 타일을 반환합니다. 정말 많은 알고리즘을 단순화합니다. –
무엇을 의미합니까? 이미 cekPosisPlayer()를 사용하여 타일 맵의 인덱스를 가져 왔습니다. 내 타일 너비가 = 60 및 높이 = 60 그 이유는 내가 이미 60으로 나눈 내 char 위치를 반환합니다. – albilaga