SDL2 MOUSEBUTTONUP触发多次一次

时间:2017-04-05 04:11:03

标签: events mouseevent sdl-2

我一直试图弄明白这一整天。我正在尝试制作一个简单的复选框。但我的MOUSEBUTTONUP事件一直触发,直到我触发另一个事件,如移动鼠标或再次单击。

//If a mouse button was released
    if (Sdl_Setup->GetMainEvent()->type == SDL_MOUSEBUTTONUP)
    {
        //If the left mouse button was pressed
        if (Sdl_Setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT)
        {
            //Get the mouse offsets
            SDL_GetMouseState(&mouseX, &mouseY);

            //If the mouse is over the button
            if ((mouseX > Button->GetX()) && (mouseX < Button->GetX() + Button->GetWidth()) && (mouseY > Button->GetY()) && (mouseY < Button->GetY() + Button->GetHeight()))
            {
                    //Set the button sprite
                    state = selected;
                    Button->SetCrop(GetFrameX(), GetFrameY(), state);
                    clicked = true;
                    std::cout << clicked << std::endl;
            }
        }
    }

这只是控制台中的垃圾邮件1(如果你不移动鼠标)我想要的只是触发一次。根据我的阅读,MOUSEBUTTONUP只应该被发送到事件队列一次

我已经尝试添加一个bool来点击它之后把它放在一个if语句中然后它可以正常工作但事情是我希望能够打开和关闭这个盒子所以当我添加一个else语句时将bool更改为false将其更改为101010而不是仅仅一个。

        //If a mouse button was released
    if (Sdl_Setup->GetMainEvent()->type == SDL_MOUSEBUTTONUP)
    {
        //If the left mouse button was pressed
        if (Sdl_Setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT)
        {
            //Get the mouse offsets
            SDL_GetMouseState(&mouseX, &mouseY);

            //If the mouse is over the button
            if ((mouseX > Button->GetX()) && (mouseX < Button->GetX() + Button->GetWidth()) && (mouseY > Button->GetY()) && (mouseY < Button->GetY() + Button->GetHeight()))
            {
                if (clicked == false)
                {
                    //Set the button sprite
                    state = selected;
                    Button->SetCrop(GetFrameX(), GetFrameY(), state);
                    clicked = true;
                    std::cout << clicked << std::endl;
                }
                else
                {
                    //Set the button sprite
                    state = noInteraction;
                    Button->SetCrop(GetFrameX(), GetFrameY(), state);
                    clicked = false;
                    std::cout << clicked << std::endl;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

Sdl_Setup中,我在制作我正在使用的窗口和类似的东西,变量mainEvent= new SDL_Event();,所以当我调用GetMainEvent()时,它是只是一遍又一遍地将其发送到PollEvent()。所以我所做的只是在void中创建一个快速Sdl_Setup函数,将mainEvent设置为空SDL_Event(),然后在clicked = true;之后调用它并且它可以正常工作。< / p>