首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过鼠标单击love2d lua递增整数

通过鼠标单击love2d lua递增整数
EN

Stack Overflow用户
提问于 2017-04-09 20:28:55
回答 2查看 129关注 0票数 0

我正在和lua/love2d一起制作一个clocker视频游戏。我发现了如何在特定区域中检测鼠标点击。问题是,当我点击时,数字增加了大约4-5个数字,即使我点击得非常快。我找不到解决方案。下面是我的代码:

代码语言:javascript
复制
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

我打赌解决方案很简单,但我被卡住了。有没有人知道这件事?谢谢。

EN

回答 2

Stack Overflow用户

发布于 2017-04-09 21:16:50

我终于知道该怎么做了。我只需要重置mouseClicked列表的x和y属性。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2017-04-10 19:01:11

您可能会发现使用love.mouse.isDown()很有用

下面是一个完整的例子,它记录了用户点击左键的次数:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43306694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档