Steph84
Steph84

Reputation: 1

Increment integer via mouse clicking love2d lua

I'm making a clocker video game with lua/love2d. I found out how to detect mouse click in a specific zone. The problem is that when I click, the number increase about 4-5 number even if I click really quickly. I wasn't be able to find the solution. Here's my code :

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

I bet the solution is simple but I'm stuck. Is anybody have a clue on that? Thanks.

Upvotes: 0

Views: 228

Answers (2)

JWT
JWT

Reputation: 407

You might find it useful to use love.mouse.isDown()

Here's a full example, which keeps track of how many times the user has left clicked:

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

Upvotes: 0

Steph84
Steph84

Reputation: 1

I finally find out how to do so. I just have to reset the x and y attributes of the mouseClicked list.

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

Upvotes: 0

Related Questions