簡體   English   中英

c# XNA 中的挑選問題

[英]Issues with picking in c# XNA

我試圖制作一個國際象棋游戲,使用采摘移動棋子,因此用戶單擊一個棋子將其設置為選定的棋子,然后在他們想要將其移動到的方格上。 這是第一次工作,但是當您單擊另一個塊以使其成為選定的塊,然后單擊一個空方塊來移動它時,它會移動第一個選定的塊。 這是我的代碼,並提前感謝任何可以提供幫助的人。

string[,] Board =
    {
        {"BRook1", "BKnight1", "BBisop1", "BQueen", "BKing", "BBishop2", "BKnight2", "BRook2"},
        {"BPawn1", "BPawn2", "BPawn3", "BPawn4", "BPawn5", "BPawn6", "BPawn7", "BPawn8"},
        {null, null, null, null, null, null, null, null },
        {null, null, null, null, null, null, null, null },
        {null, null, null, null, null, null, null, null },
        {null, null, null, null, null, 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,因為除非你真的快速點擊,否則鼠標仍然是向下的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM