Search code examples
lualove2d

Love2d isDown equivalent for touch control?


I have this code for mouse:

if love.mouse.isDown(1) then
   self.player:fire()
end

So this fires the gun as long as the mouse button is held down. I have onscreen buttons for touch which I can capture individual touches with love.touchpressed but this fires the gun one at a time.

I want it so that the player can touch the onscreen button and as long as it is held down the gun fires continuously.


Solution

  • Use:

    if next(love.touch.getTouches()) ~= nil then
        self.player:fire()
    end
    

    or define function is_empty or any:

    function is_empty(t)
        return next(t) == nil
    end
    
    if not is_empty(love.touch.getTouches()) then
        self.player:fire()
    end
    
    function any(t)
        return next(t) ~= nil
    end
    
    if any(love.touch.getTouches()) then
        self.player:fire()
    end