C# XNA如何使用鼠标选择瓷砖 public void Draw(SpriteBatch-SpriteBatch) { 对于(int x=0;x

C# XNA如何使用鼠标选择瓷砖 public void Draw(SpriteBatch-SpriteBatch) { 对于(int x=0;x,c#,select,xna,mouse,tile,C#,Select,Xna,Mouse,Tile,我有一个随机瓷砖引擎,我想知道如何使瓷砖周围有一个黑色的方形纹理,并通过点击它们来选择。以及当我点击它时我如何改变瓷砖纹理 public void Draw(SpriteBatch spriteBatch) { for(int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { spriteBatch.Draw

我有一个随机瓷砖引擎,我想知道如何使瓷砖周围有一个黑色的方形纹理,并通过点击它们来选择。以及当我点击它时我如何改变瓷砖纹理

public void Draw(SpriteBatch spriteBatch)
    {
        for(int x = 0; x < 10; x++) 
        {
            for (int y = 0; y < 10; y++)
            {
                spriteBatch.Draw(tiles[index[x, y]], tileRect = new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight), 
                                 Color.White);
            }
        }
    }
您需要:

一个单独的黑色方形“瓷砖”,您可以根据需要在顶部绘制。例:

I am wondering how I could make the tiles have a black square texture around it.
and selectable by clicking on them. 
and how I could change that tile texture when I click on it.
要处理鼠标单击。例:

private Vector2 mSelectedTileCoordinate = new Vector2();
将单击的屏幕坐标转换为地图平铺坐标。例:

public void Update(GameTime pGameTime)
{
   ...
   MouseState mouseState = Mouse.GetState();
   Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);

   if (mouseState.LeftButton == ButtonState.Pressed)
      DoMouseClick(mousePosition);
   ...
}
public void DoMouseClick(Vector2 pMouseXY)
{
   ...
   Vector2 tileXY = ScreenToTile(pMouseXY);
   ...
}

private Vector2 ScreenToTile(Vector2 pScreenXY)
{
   // you need to get the position of the map here
   // ex: if the 'camera' is looking at (100, 100), then the map is drawn to (-100, -100)
   Vector2 mapOffset = GetMapOffset();

   // you may need to add or subtract depending what value you are using
   // if mapOffset is the coordinate you are 'looking at', add
   // if mapOffset is the coordinate that the map is being drawn to, subtract
   Vector2 mapXY = pScreenXY +/- mapOffset;

   // you need to get the width and height of the tiles
   Vector2 tileSize = GetTileSize();

   // this should now be the tile coordinate
   // you may or may not want to have rounded the XY values as well
   Vector2 tileXY = mapXY / tileSize;
   return new Vector2((int)tileXY.X, (int)tileXY.Y);
}
根据单击的坐标更改选定的平铺。例:

public void Update(GameTime pGameTime)
{
   ...
   MouseState mouseState = Mouse.GetState();
   Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);

   if (mouseState.LeftButton == ButtonState.Pressed)
      DoMouseClick(mousePosition);
   ...
}
public void DoMouseClick(Vector2 pMouseXY)
{
   ...
   Vector2 tileXY = ScreenToTile(pMouseXY);
   ...
}

private Vector2 ScreenToTile(Vector2 pScreenXY)
{
   // you need to get the position of the map here
   // ex: if the 'camera' is looking at (100, 100), then the map is drawn to (-100, -100)
   Vector2 mapOffset = GetMapOffset();

   // you may need to add or subtract depending what value you are using
   // if mapOffset is the coordinate you are 'looking at', add
   // if mapOffset is the coordinate that the map is being drawn to, subtract
   Vector2 mapXY = pScreenXY +/- mapOffset;

   // you need to get the width and height of the tiles
   Vector2 tileSize = GetTileSize();

   // this should now be the tile coordinate
   // you may or may not want to have rounded the XY values as well
   Vector2 tileXY = mapXY / tileSize;
   return new Vector2((int)tileXY.X, (int)tileXY.Y);
}
并在绘制代码中绘制平铺。例:

public void DoMouseClick(Vector2 pMouseXY)
{
   ...
   Vector2 tileXY = ScreenToTile(pMouseXY);

   mSelectedTileCoordinate = tileXY;
}
public void Draw(SpriteBatch-SpriteBatch)
{
对于(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
spriteBatch.Draw(平铺[索引[x,y]],平铺竖立=新矩形(x*平铺宽度,y*平铺高度,平铺高度,平铺高度),
颜色(白色);
}
}
//抽签选择
Vector2 screenXY=TileToScreen(mSelectedTileCoordinate);
矩形绘图区域=新矩形(屏幕XY.X、屏幕XY.Y、平铺宽度、平铺高度);
spriteBatch.Draw(mBlackTile、drawArea、Color.White);
}
专用Vector2 TileToScreen(Vector2 pTileXY)
{
//这与ScreenToTile相反
Vector2 tileSize=GetTileSize();
Vector2 mapXY=pTileXY*tileSize;
Vector2 mapOffset=GetMapOffset();
//您必须以与ScreenToTile()相反的方式执行此操作
矢量2屏幕XY=mapXY+/-mapOffset;
返回屏幕XY;
}

由于您将磁贴存储在一个数组中,因此您可以使用以下方法来执行此操作:

public void Draw(SpriteBatch spriteBatch)
{
    for(int x = 0; x < 10; x++) 
    {
        for (int y = 0; y < 10; y++)
        {
            spriteBatch.Draw(tiles[index[x, y]], tileRect = new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight), 
                             Color.White);
        }
    }

    // draw selection
    Vector2 screenXY = TileToScreen(mSelectedTileCoordinate);
    Rectangle drawArea = new Rectangle(screenXY.X, screenXY.Y, tileWidth, tileHeight);
    spriteBatch.Draw(mBlackTile, drawArea, Color.White);
}

private Vector2 TileToScreen(Vector2 pTileXY)
{
   // this does the reverse of ScreenToTile

   Vector2 tileSize = GetTileSize();
   Vector2 mapXY = pTileXY * tileSize;       

   Vector2 mapOffset = GetMapOffset();

   // you'll have to do this the opposite way from ScreenToTile()
   Vector2 screenXY = mapXY +/- mapOffset;       

   return screenXY;
}
/16
表示以像素为单位的瓷砖大小,出于某种原因,在我的游戏中,我必须在y值上加+1,对你来说可能不是这样

要添加黑色纹理,您可以随时创建一个,也可以将其加载到LoadContent()中

然后像这样画

        MouseState ms = Mouse.GetState(); 

        if (ms.LeftButton == ButtonState.Pressed)
        {
            int x = Convert.ToInt32(ms.X) /16;
            int y = Convert.ToInt32(ms.Y) /16 +1;

            tiles[x, y] = //Left button Clicked, Change Texture here!

        }
        if (ms.RightButton == ButtonState.Pressed)
        {
            int x = Convert.ToInt32(ms.X) / 16;
            int y = Convert.ToInt32(ms.Y) / 16 + 1;

            tiles[x, y] = //Right button Clicked, Change Texture here!


        }

你有什么类型的瓷砖(正方形、六角形、等角形等)我有一个选择纹理,我知道所有这些。这是我做那些我不理解的事情的方式。这就是我问的原因。我不知道您编写了哪些类,但我将尝试使用XNA对象进行详细说明。谢谢您的精彩解释。这让我困惑的是如何使用数组。这应该对我有帮助。我明天醒来时会试试看。我接受了你的回答。没问题,你应该在瓷砖上添加一个纹理值,然后作为瓷砖[x,y]来做。纹理=SomeTexture。我还编辑了它,这是一种更简单的方法。我应该怎样做[]。纹理?我以前从来没有这样做过,因为我会为瓷砖上一堂课,就像我刚才问的一个问题一样,为每个单独的平铺指定一个纹理,或者执行类似于if Tiles[x,y].blocktype=blackBlock{//绘制一个黑色纹理}的操作。好的,我几乎完成了所有设置。只是想知道如何只选择一个瓷砖,因为当我单击它时,会删除所有瓷砖,我需要知道如何获得瓷砖位置来绘制选择。