Love2d 光标位置

Love2d cursor positions

我对 love2d(lua 脚本) 游标函数有疑问。我不想创建一个区域来单击以​​执行操作。

我开始跟踪 x 和 y 参数中的 for 循环。我想到的唯一另一个问题是它是否会通过 number/coordinates 的 for 循环并在 love.mouse.get() 结束的 1 个数字上结束并允许光标最终被点击在最后一个坐标(一个像素)上。

for r = 660, 770 do --the x coordinates
mx = love.mouse.getX(r)
end

for f = 99.33, 169.66 do  --the y coordinates
my = love.mouse.getY(f)
end

以及我将如何组合两个 for 循环变量(r 和 f)。

总而言之,我希望能够点击一个区域并执行一个操作。我知道没有 love.load、love.update 和 love.draw 函数,因为这只是一个测试文件,用于了解这一切是如何工作的。

谢谢:)

你想多了。你真正想做的是定义一个 minimum 和一个 maximum 两个维度,监听鼠标事件,然后检查鼠标位置是否是在你的边界内。无需遍历整个范围。

考虑这个示例 'game',我们在其中绘制了一个简单的红色框,单击该框会切换左上角文本的显示。

local box_dims = {
    { 660, 770 },
    { 99.33, 169.66 }
}

local show = false

function love.mousepressed (x, y)
    if
        x >= box_dims[1][1] and
        x <= box_dims[1][2] and
        y >= box_dims[2][1] and
        y <= box_dims[2][2] then

        show = not show
    end
end

function love.draw ()
    love.graphics.setColor(255, 0, 0, 255)

    love.graphics.rectangle('fill',
        box_dims[1][1], box_dims[2][1],
        box_dims[1][2] - box_dims[1][1],
        box_dims[2][2] - box_dims[2][1]
    )

    if show then
        love.graphics.print('hello world', 10, 10)
    end
end

查看文档以确定哪种鼠标事件适合您。