Page 1 of 1

Love2d touch functions?

Posted: Tue Apr 30, 2019 2:27 pm
by miniaturedog
The title was too short to explain my problem fully, so here it is:
Right now, keyboard movement controls for my game are working perfectly ("a" to move left, "d" to move right, "space" to jump). However, I also want to test movement on my Android phone, and I'm not sure how Love2d touch functions work (as the wiki isn't very clear). For example, after I get a camera set up (currently in progress), my player should be centered in the middle of the screen. A touch and hold on the left side of the screen would make the player move to the right, and a touch and hold on the right should make the player move to the left, with doubletap to jump. As soon as touch is released, the player should stop moving. Any suggestions as to how I should set this up?

Re: Love2d touch functions?

Posted: Tue Apr 30, 2019 8:36 pm
by zorg
What parts of the wiki are unclear?

Re: Love2d touch functions?

Posted: Tue Apr 30, 2019 10:31 pm
by miniaturedog
zorg wrote: Tue Apr 30, 2019 8:36 pm What parts of the wiki are unclear?
Essentially all of the love.touch section, for a beginner like me. I'm specifically not sure how to check if a touch is within a certain area of the screen (and make the player move if so), or how to use touch ids.

Re: Love2d touch functions?

Posted: Tue Apr 30, 2019 10:41 pm
by pgimeno
If you do not need handling simultaneous touches, you don't need to use touch; you can just use the mouse functions.

If you need to handle simultaneous touches, the only significant difference between the mouse functions and the touch functions is that each touch (each finger in contact with the screen) is assigned an id, which you're given.

You can track how many touches there are, and at which coordinates, by using the id as the key of a table.

Untested:

Code: Select all

local touches = {}

function love.touchpressed(id, x, y)
  touches[id] = {x, y}
end

function love.touchmoved(id, x, y)
  touches[id][1] = x
  touches[id][2] = y
end

function love.touchreleased(id, x, y)
  touches[id] = nil
end

local function howManyTouches()
  return #touches
end

function isRectangleBeingTouched(rect)
  for k, v in pairs(touches) do
    if rect:containsPoint(v[1], v[2]) then
      return true
    end
  end
  return false
end

Re: Love2d touch functions?

Posted: Sun Sep 20, 2020 3:59 pm
by Lekic
Please am creating a game on android platform can i get the code for touch i.e touchPressed etc stuffs like dat and site where i can get it please

Re: Love2d touch functions?

Posted: Sun Apr 07, 2024 1:10 am
by SuskyTheCorgi
miniaturedog wrote: Tue Apr 30, 2019 2:27 pm The title was too short to explain my problem fully, so here it is:
Right now, keyboard movement controls for my game are working perfectly ("a" to move left, "d" to move right, "space" to jump). However, I also want to test movement on my Android phone, and I'm not sure how Love2d touch functions work (as the wiki isn't very clear). For example, after I get a camera set up (currently in progress), my player should be centered in the middle of the screen. A touch and hold on the left side of the screen would make the player move to the right, and a touch and hold on the right should make the player move to the left, with doubletap to jump. As soon as touch is released, the player should stop moving. Any suggestions as to how I should set this up?
I am no Love2D expert, (in fact I just joined) but it might use the mouse and you just have to add an if clicked statement for a mobile version.