我正在和lua/love2d一起制作一个clocker视频游戏。我发现了如何在特定区域中检测鼠标点击。问题是,当我点击时,数字增加了大约4-5个数字,即使我点击得非常快。我找不到解决方案。下面是我的代码:
function love.mousepressed(x, y, button, istouch)
if button == 1 then
mouseClicked.on = true
mouseClicked.x = x
mouseClicked.y = y
end
end
function love.mousereleased(x, y, button, istouch)
if button == 1 then
mouseClicked.on = false
mouseClicked.x = nil
mouseClicked.y = nil
end
end
function Control.Update(ppDt, pIncrement)
local i
for i = 1, #listButtons do
local b = listButtons[i]
--if b.isEnabled == true then -- if the button is showing
if mouseClicked.on == true then -- if the player click
if mouseClicked.x > b.x - tileWidth/2 and
mouseClicked.x < b.x + tileWidth/2 then
if mouseClicked.y > b.y - tileHeight/2 and
mouseClicked.y < b.y + tileHeight/2 then
b.position = "down" -- if the button is clicked, button down
if b.id == 1 then pIncrement = pIncrement + 1 end
end
end
else b.position = "up" end -- if the player doesn t click, button back up
--end
end
return pIncrement
end我打赌解决方案很简单,但我被卡住了。有没有人知道这件事?谢谢。
发布于 2017-04-09 21:16:50
我终于知道该怎么做了。我只需要重置mouseClicked列表的x和y属性。
function Control.Update(ppDt, pIncrement)
local i
for i = 1, #listButtons do
local b = listButtons[i]
--if b.isEnabled == true then -- if the button is showing
if mouseClicked.on == true then -- if the player click
if mouseClicked.x > b.x - tileWidth/2 and
mouseClicked.x < b.x + tileWidth/2 then
if mouseClicked.y > b.y - tileHeight/2 and
mouseClicked.y < b.y + tileHeight/2 then
b.position = "down" -- if the button is clicked, button down
if b.id == 1 then
pIncrement = pIncrement + 1
-- to stop increment without stopping animation
mouseClicked.x = 0
mouseClicked.y = 0
end
end
end
else b.position = "up" end -- if the player doesn t click, button back up
--end
end
return pIncrement
end发布于 2017-04-10 19:01:11
您可能会发现使用love.mouse.isDown()很有用
下面是一个完整的例子,它记录了用户点击左键的次数:
local clickCount, leftIsDown, leftWasDown
function love.load()
clickCount = 0
leftIsDown = false
leftWasDown = false
end
function love.update(t)
leftIsDown = love.mouse.isDown(1)
if leftIsDown and not leftWasDown then
clickCount = clickCount + 1
end
leftWasDown = leftIsDown -- keep track for next time
end
function love.draw()
local scr_w, scr_h = love.graphics.getDimensions()
love.graphics.print('Left clicked ' .. clickCount .. ' times', scr_w/3, scr_h/3, 0, 1.5)
endhttps://stackoverflow.com/questions/43306694
复制相似问题