Events SDL2鼠标按钮多次触发
我一整天都在想这个问题。我想做一个简单的复选框。但是我的MOUSEBUTTONUP事件会一直触发,直到我触发另一个事件,如移动鼠标或再次单击Events SDL2鼠标按钮多次触发,events,mouseevent,sdl-2,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->
//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;
}
}
}
//如果释放鼠标按钮
如果(Sdl_设置->GetMainEvent()->type==Sdl_鼠标按钮)
{
//如果按下鼠标左键
如果(Sdl_设置->GetMainEvent()->button.button==Sdl_按钮左)
{
//获取鼠标偏移量
SDL_GetMouseState(&mouseX,&mouseY);
//如果鼠标在按钮上
如果((mouseX>Button->GetX())&&&(mouseXGetX()+Button->GetWidth())&&(mouseY>Button->GetY())&&&(mouseYGetY()+Button->GetHeight())
{
//设置按钮精灵
状态=已选择;
按钮->设置裁剪(GetFrameX(),GetFrameY(),状态);
单击=真;
std::cout在Sdl_Setup
中是我创建我正在使用的窗口之类的东西的地方,变量mainEvent
是=new Sdl_Event();
所以当我调用GetMainEvent()
时,它只是将它发送到PollEvent()
一次又一次。所以我所做的就是在Sdl\u设置中创建一个快速的void
函数,将mainEvent
设置为一个空白的Sdl\u事件()
,然后在单击=true;
后调用它,它就工作了。出现了Sdl\u设置->GetMainEvent()
返回最新事件。您可能需要一种方法将其重置为“无效”状态。@keltar就是这样!完全有道理。谢谢您的帮助
//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;
}
}
}
}