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);
}
}
}
我有一个随机的磁贴引擎,我想知道如何让磁贴周围有黑色方形纹理并通过点击它们来选择。以及当我点击它时如何改变那个瓷砖纹理。
答案 0 :(得分:2)
由于您将瓷砖存储在数组中,您可以使用以下内容来执行此操作:
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)
答案 1 :(得分:0)
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;
}