通过鼠标点击 love2d lua 增加整数
2017-4-9 12:28:55
收藏:0
阅读:67
评论:2
我正在用 lua/love2d 制作时钟视频游戏。 我发现如何检测特定区域的鼠标点击。问题是,即使我很快地点击,数字也增加了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
我敢打赌解决方案很简单,但我被卡住了。有人知道吗? 谢谢。
点赞
用户2969217
你可能会发现使用 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)
end
2017-04-10 11:01:11
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我终于找到了如何实现的方法。 我只需要重置 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 mouseClicked.on == true then -- 如果玩家点击 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 b.id == 1 then pIncrement = pIncrement + 1 -- 防止动画停止而不止增加 mouseClicked.x = 0 mouseClicked.y = 0 end end end else b.position = "up" end -- 如果玩家没有点击,按钮恢复原位 --end end return pIncrement end