微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 C# XNA 中选择的问题

如何解决在 C# XNA 中选择的问题

我正在尝试制作国际象棋游戏,使用采摘来移动棋子,因此用户单击一个棋子将其设置为所选棋子,然后在他们想要将其移动到的方格上。这是第一次起作用,但是当您单击不同的棋​​子以使其成为选定的棋子,然后单击一个空方块来移动它时,它会移动第一个选定的棋子。这是我的代码,在此先感谢任何可以提供帮助的人。

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,因为除非你点击速度非常快,否则鼠标仍然处于按下状态。

>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。