Love2d touch functions?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

Love2d touch functions?

Post 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?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Love2d touch functions?

Post by zorg »

What parts of the wiki are unclear?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

Re: Love2d touch functions?

Post 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.
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Love2d touch functions?

Post 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
Lekic
Prole
Posts: 5
Joined: Tue Sep 15, 2020 9:56 pm

Re: Love2d touch functions?

Post 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
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Re: Love2d touch functions?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], veethree and 81 guests