等待用户抬起鼠标 - 无法工作:Love2D

我正在使用Love2D引擎创建一个程序,用户点击后返回鼠标当前位置的坐标。然而,在返回另一个位置之前,必须“取消点击”鼠标,然后单击下一个所需位置。

我在下面粘贴了应处理此操作的脚本:

function scripts.waitForMouseLift()
    while love.mouse.isDown("l", "r") do
        --Stays in a loop until user releases mouse, then lets the program continue
    end
end

这应该在技术上可行,因为循环将在释放鼠标点击时结束,但实际上它只是继续进入无限循环,不管我之前单击了哪个鼠标按钮。

因此,我的问题分为两个部分:首先,有没有办法使这种方法起作用?其次,是否有任何替代方案或更好的解决方案来解决这个问题?

点赞
用户2726734
用户2726734

Love使用回调函数实现这个功能,你需要的是love.mousereleased,你可能也需要看一下love.mousepressed。这些函数是你添加在脚本里面,每当用户点击(或释放)鼠标按钮时,函数就会被调用。所以你不需要一直检查鼠标是否改变状态,并且你不能在繁忙的循环中等待它,因为你需要把控制权交回给Love,以便它有机会更新鼠标状态。

function love.mousepressed(x, y, button)
  -- do something with x, y
  print("Mouse Pressed", button, "at", x, y)
end

function love.mousereleased(x, y, button)
  print("Mouse Released", button, "at", x, y)
end
2015-01-10 01:18:16