简体   繁体   中英

Double Single Click in XNA

I'm making a chess game and I made a 2 2D array of vector2D to hold the position of the pieces and then when click on it first then on the board second it should change position but I got 2 problems.

first : I have to click one Box above the required one second : The 2 single Clicked reads at the same time so when I click on the Pieces it just change it's position to the place where I clicked it from

here's my code :

  public void ChangePositionAfterDrag()
        {
            bool CheckifFound=false,Black=false,White=false ; // if the loops hits the _mousedownposition and which kind was in the box
                                // save the index

       /*     for (int i = 100; i < 100+(80*8); i += 80)
            {
                for (int j = 80; j < 80 + (80 * 8); j += 80)
                {*/

            lastMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
            if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
            {
                if (CheckifFound == false)
                {
                    for (int k = 0; k < 2; k++)
                    {
                        for (int l = 0; l < 8; l++)
                        {
                            if (currentMouseState.X > _BlackPiecesPosition[k, l].X && currentMouseState.X < _BlackPiecesPosition[k, l].X + 80 && currentMouseState.Y < _BlackPiecesPosition[k, l].Y && currentMouseState.Y > _BlackPiecesPosition[k, l].Y-80 )
                            {
                                CheckifFound = true;
                                IndX = k;
                                IndY = l;
                                Black = true;
                                break;
                            }
                            if (currentMouseState.X > _WhitePiecesPosition[k, l].X && currentMouseState.X < _WhitePiecesPosition[k, l].X + 80 && currentMouseState.Y < _WhitePiecesPosition[k, l].Y && currentMouseState.Y > _WhitePiecesPosition[k, l].Y -80 )
                            {
                                CheckifFound = true;
                                IndX = k;
                                IndY = l;
                                White = true;
                                break;
                            }
                        }
                        if (CheckifFound == true)
                            break;
                    }
                }
            }
            LastMouseState2 = CurrentMouseState2;
            CurrentMouseState2 = Mouse.GetState();
            if (LastMouseState2.LeftButton == ButtonState.Pressed && CurrentMouseState2.LeftButton == ButtonState.Released)
            {
                NewPosition.X = LastMouseState2.X;
                NewPosition.Y = LastMouseState2.Y;
            }

            if(Black==true)
            {
                _BlackPiecesPosition[IndX, IndY].X = NewPosition.X;
                _BlackPiecesPosition[IndX, IndY].Y = NewPosition.Y;
            }
            else if (White == true)
            {
                    _WhitePiecesPosition[IndX, IndY].X = NewPosition.X;
                    _WhitePiecesPosition[IndX, IndY].Y = NewPosition.Y;
            }


        }

You are trying to take the piece and drop it in the same function. That's why you get 2 clicks at the same time.

You need to create a Take() and Drop() function, and call them when needed. To know witch is the next function that should be called you could use a simple boolean variable.

The code structure should be something like this:

bool isLastCalledTake = false;
protected override void Update (GameTime gameTime)
{
    //whtever are your first steps in update...
    if(/*user clicked on table logic here*/)
    {
        if(/*left button is clicked logic here*/)
        {
            if(isLastCalledTake)
            {
                isLastCalledTake = false;
                Drop();
            }
            else
            {
                isLastCalledTake = true;
                Take();
            }
        }
    }
}

And so, when you Take() , you should check if there is anything to take, is the right color figure taken, etc., and when you Drop() , check if the figure is dropped on the table, on the possible place, on the other figure, etc.

This way you are checking every click once (not twice as in your function), and doing Drop() or Take() . This way code and logic are simplified.

EDIT: Alternatively, create one function witch will decide weather to take or drop the peace, and call that function on each mouse click

void OnMouseClick (bool take)
{
    if(take)
        /* take logic here */
    else
        /* drop logic here */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM