XNA 2D 图块引擎鼠标点击问题

本文关键字:问题 鼠标 引擎 2D XNA | 更新日期: 2023-09-27 17:56:32

我的问题是点击只在右下角获得寄存器,在某些情况下,即使没有,它似乎会变得更糟,你偏离 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;
    }

XNA 2D 图块引擎鼠标点击问题

以下是我喜欢处理单击瓦片地图的方式。

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