【问题标题】:Double Single Click in XNA在 XNA 中双击
【发布时间】:2016-04-26 04:22:58
【问题描述】:

我正在制作一个国际象棋游戏,我制作了一个 2D 的 vector2D 数组来保存棋子的位置,然后当首先单击它然后在棋盘上第二个它应该改变位置,但我遇到了 2 个问题。

首先:我必须单击所需框上方的一个框 第二:2个单次点击同时读取,所以当我点击片段时,它只是将它的位置更改为我点击它的位置

这是我的代码:

  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;
            }


        }

【问题讨论】:

  • 也许你可以添加一些图片/截图来解释你的意图是什么,你的问题是什么?你的描述对我来说太模糊了。

标签: c# oop xna


【解决方案1】:

您正试图将这部分内容放到同一个函数中。这就是您同时获得 2 次点击的原因。

您需要创建一个Take()Drop() 函数,并在需要时调用它们。要知道 witch 是应该调用的下一个函数,您可以使用一个简单的布尔变量。

代码结构应该是这样的:

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();
            }
        }
    }
}

所以,当你Take()时,你应该检查是否有东西要拿,是否采取了正确的颜色数字等等,当你Drop()时,你应该检查数字是否掉在桌子上,on可能的地方,在另一张图上等等。

这样,您将检查每次点击一次(而不是您的函数中的两次),然后执行Drop()Take()。这样代码和逻辑就被简化了。

编辑: 或者,创建一个函数,女巫将决定天气是否和平,并在每次鼠标点击时调用该函数

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

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多