XNA 2D TileEngine鼠标点击问题

时间:2011-05-15 07:33:46

标签: c# xna

我的问题是,点击只会在右下角获得注册,在某些情况下甚至不会出现更糟糕的情况,你离开0.0的时间越长越好,就越糟糕了。

   public void Render(SpriteBatch B, Camera C)
    {
        Vector2 firstSquare = new Vector2(C.Position.X / 32, C.Position.Y / 32);
        int firstX = (int)firstSquare.X;
        int firstY = (int)firstSquare.Y;

        Vector2 squareOffset = new Vector2(C.Position.X % 32, C.Position.Y % 32);
        int offsetX = (int)squareOffset.X;
        int offsetY = (int)squareOffset.Y;
        for (int y = 0; y < 16; y++)
        {
            for (int x = 0; x < 26; x++)
            {
                Tile T = GetTile(x + firstX, y + firstY);
                if (T == null)
                {
                    continue;
                }
                T.RenderWithCamera(B,new Vector2((x*32)-offsetX,(y*32)-offsetY));

            }
        }   
   public void CheckClick(float mx, float my,Camera C)
    {
        Vector2 firstSquare = new Vector2(C.Position.X / 32, C.Position.Y / 32);
        int x = (int)firstSquare.X;
        int y = (int)firstSquare.Y;
        Vector2 squareOffset = new Vector2(C.Position.X % 32, C.Position.Y % 32);
        int offsetX = (int)squareOffset.X;
        int offsetY = (int)squareOffset.Y;
        int vx = (int)mx / 32;
        int vy = (int)my / 32;
        float x1 = vx + x;
        float y1 = vy + y;
        int maxX, maxY;
        maxX = C.Width / 32;
        maxY = C.Height / 32;
        Console.WriteLine("MAX_X:" + maxX + "MAX_Y:" + maxY);
        Tile T = GetTile(x1, y1);
        Rectangle A = new Rectangle((int)mx, (int)my, 1, 1);
        if (T == null)
        { Console.WriteLine("No Tile found"); return; }
        if (T.IsInside(A))
        {
            Console.WriteLine("Not inside?");
            Tile S = null;
            S = new Wall((int)x1, (int)y1, 0);
            if (S != null)
            {

                tiles.Add(S);
                tiles2[(int)T.pos.X, (int)T.pos.Y] = S;
            }

        }
        Console.WriteLine("Clicked Tile at X:" + T.pos.X + "Y:" + T.pos.Y);
    }
    public bool IsInside(Rectangle B) // TILE
    {
        Rectangle rectA = new Rectangle((int)Last_pos.X, (int)Last_pos.Y, icon.Width, icon.Height);

        Console.WriteLine("A:" + rectA.X + "A.y:" + rectA.Y + "B.X:" + B.X + "B.Y:" + B.Y);
        if(rectA.Intersects(B))
        {
            return true;
        }
        else
            return false;
    }

1 个答案:

答案 0 :(得分:1)

以下是我喜欢处理点击瓷砖地图的方法。

int xTile = Math.floor((Mouse.X + CameraBounds.left) / Tile.width);
int yTile = Math.floor((Mouse.Y + CameraBounds.top) / Tile.height);