在XNA中使用tilemap进行碰撞检测

时间:2013-06-09 07:18:48

标签: c# windows-phone-7 xna

这是我的tilemap代码。

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;
                }                               
            }
        }
    }
}

所以我设置每个tile都有index和boolean passable。这是我移动精灵的方法,但总是遇到错误。

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 :
        }
    }
}

它只检查正确的运动一段时间,如果我将我的字符向右移动它会崩溃。有什么不对?或者告诉我你是否需要更多解释或代码。

1 个答案:

答案 0 :(得分:1)

确定。我现在自己解决了。检查我的动作我错了。它应该是

if (myMap.Rows[(int)cekPosisiPlayer().Y].Columns[(int)(cekPosisiPlayer().X + (7 / 6))].passable)

因此它将检测下一个tile索引是否可以通过。抱歉,添麻烦了。谢谢。 :d