【问题标题】:XNA how to select tiles with mouseXNA如何用鼠标选择瓷砖
【发布时间】:2013-01-21 23:29:16
【问题描述】:
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);
            }
        }
    }

我有一个随机图块引擎,我想知道如何使图块周围有黑色方形纹理,并且可以通过单击它们进行选择。以及当我单击它时如何更改该瓷砖纹理。

【问题讨论】:

  • 你有什么类型的瓷砖(正方形、六边形、等距等)

标签: c# select xna mouse tile


【解决方案1】:

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

        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!


        }

/ 16 用于以像素为单位的图块大小,出于某种原因,在我的游戏中,我必须在 y 值上加上 +1,对你来说可能不是这样。

要添加黑色纹理,您可以在旅途中创建一个,或者在 LoadContent() 中加载它

然后像这样画出来;

if (tiles[x,y].HasBlackTexture = true)
     spriteBatch.Draw(blah,Color.Black)

【讨论】:

  • 感谢您的精彩解释。这让我感到困惑的是如何使用数组来做到这一点。这应该可以帮助我。明天我起床后我会试试看。我接受了您的回复。
  • 没问题,你应该给瓷砖添加一个纹理值,就像 Tiles[x,y].Texture = SomeTexture.我也编辑了它,它是一种更简单的方法。
  • 我将如何做 [].Texture?我以前从来没有这样做过
  • 我会为瓷砖开设一个课程,类似于我不久前提出的一个问题。 stackoverflow.com/questions/9557712/…,为每个单独的图块分配一个纹理,或执行类似 if Tiles[x,y].blocktype = blackBlock { //绘制黑色纹理 }
  • 好的,我已经完成了一切设置。只是弄清楚如何只选择一个图块,因为当我单击它时会删除所有图块,我需要弄清楚如何获取图块位置以绘制选择。
【解决方案2】:
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 Texture2D mBlackTile;
...
public void LoadContent()
{
   ...
   mBlackTile = ContentManager.Load<Texture2D>("blackTile");
   ...
}

对所选图块的引用(坐标),将使用它以便您知道在哪里绘制黑色方形图块。例如:

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 DoMouseClick(Vector2 pMouseXY)
{
   ...
   Vector2 tileXY = ScreenToTile(pMouseXY);

   mSelectedTileCoordinate = tileXY;
}

并在您的绘图代码中绘制图块。例如:

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

【讨论】:

  • 我有一个选择纹理,我知道这一切。这是我做那些我不理解的事情的方式。这就是我问的原因。
  • 我不知道你写了哪些类,但我会尝试使用 XNA 对象详细说明。
猜你喜欢
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 2019-07-04
相关资源
最近更新 更多