2011-05-15 89 views
1

我的问题是,点击只能得到右下角的寄存器,在某些情况下,即使在那里,它似乎变得更糟,你偏离0.0的时间越长,喜欢的越差。XNA 2D TileEngine鼠标点击问题

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

CheckClick是从哪里调用的?看起来像从哪里来的将是执行代码。 – FreeAsInBeer 2011-05-16 12:07:41

回答

1

下面是我喜欢如何处理单击瓷砖地图。

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