问题描述
我正在尝试制作国际象棋游戏,使用采摘来移动棋子,因此用户单击一个棋子将其设置为所选棋子,然后在他们想要将其移动到的方格上。这是第一次起作用,但是当您单击不同的棋子以使其成为选定的棋子,然后单击一个空方块来移动它时,它会移动第一个选定的棋子。这是我的代码,在此先感谢任何可以提供帮助的人。
string[,] Board =
{
{"brook1","BKnight1","BBisop1","BQueen","BKing","BBishop2","BKnight2","brook2"},{"BPawn1","BPawn2","BPawn3","BPawn4","BPawn5","BPawn6","BPawn7","BPawn8"},{null,null,null },{"WPawn1","WPawn2","WPawn3","WPawn4","WPawn5","WPawn6","WPawn7","WPawn8"},{"WRook1","WKnight1","WBishop1","WQueen","WKing","WBishop2","WKnight2","WRook2"},};
MouseState mouseState = Mouse.GetState();
decimal decMousex = mouseState.X;
decimal decMousey = mouseState.Y;
y = Convert.ToInt32(Math.Floor(decMousex / 75));
x = Convert.ToInt32(Math.Floor(decMousey / 75));
if (mouseState.LeftButton == ButtonState.pressed
&& Board[x,y] != null && pieceSelected == null)
{
pieceSelected = Board[x,y];
selectedX = x;
selectedY = y;
}
else if (mouseState.LeftButton == ButtonState.pressed
&& Board[x,y] == null && pieceSelected != null)
{
Board[selectedX,selectedY] = null;
Board[x,y] = pieceSelected;
pieceSelected = null;
}
解决方法
看看当鼠标在一个空方块上按下时发生了什么,pieceSelected 会移动到那里,但下一次马上又变成了pieceSelected,因为除非你点击速度非常快,否则鼠标仍然处于按下状态。
>