gpt4 book ai didi

lua - 如何在 Love2D 中使用定向鼠标点击功能?

转载 作者:行者123 更新时间:2023-12-02 04:23:39 29 4
gpt4 key购买 nike

我正在尝试这样做,以便如果用户在 200x300 处单击鼠标左键,则会更改变量。我在网上搜索了教程但没有找到解决方案。

function love.load()
medium = love.graphics.newFont(45)
small = love.graphics.newFont(25)
micro = love.graphics.newFont(14)

StartGame = false
end

function love.draw()
love.graphics.setColor(255, 0, 0)
love.graphics.setFont(small)
love.graphics.print("Play", 200, 300)
end

function love.mousepressed(x, y, button)
if love.mousepressed( 200, 300, 'l' ) then
StartGame = true
end
end

function love.mousereleased(x, y, button)
end

function love.quit()
end

最佳答案

当用户点击 200、300 时,这将设置变量

function love.mousepressed(x, y, button)
if button == "l" and x == 200 and y == 300 then
StarGame = true
end
end

但这对用户来说可能要求太高,无法完美地选择单个点。因此,下面的代码在点 (200, 300) 周围添加了 10 个像素,以便于单击。

local function inCircle(cx, cy, radius, x, y)
local dx = cx - x
local dy = cy - y
return dx * dx + dy * dy <= radius * radius
end


function love.mousepressed(x, y, button)
if button == "l" and inCircle(200, 300, 10, x, y) then
StartGame = true
end
end

尝试更改 10 来找到合适的值。

关于lua - 如何在 Love2D 中使用定向鼠标点击功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575379/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com