Mouse or Touch function for mobile?

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.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Mouse or Touch function for mobile?

Post by hasen »

Should I be using this:

Code: Select all

love.mousepressed( x, y, button, istouch )
or indeed this:

Code: Select all

love.touchpressed( id, x, y, dx, dy, pressure )
...for touches on mobile games? I need to be able to handle up to three simultaneous touches.

And in fact I just noticed there's also:

Code: Select all

love.touch.getTouches
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Mouse or Touch function for mobile?

Post by raidho36 »

The "getTouches" is the same as "isDown" by principle - returns what's currently going on, and you will need (a lot) of additional code to detect rising or falling edge on the input. Conversely, the events fire on rising and falling edge and you will need your own code to keep track of their status between updates. The mouse functionality is technically not available on mobile unless a mouse is attached. But for convenience's sake, the mouse events are mapped to the first touch input, so you can handle single touch the same way you handle a mouse.

I have a library for touch input that, among other things, supports designating on-screen zones in which the input should be captured. It's a bit verbose and has steep learning curve but it is a very powerful tool.
https://github.com/raidho36/love-multitouch
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Mouse or Touch function for mobile?

Post by hasen »

I noticed mousepressed also captures touches though and if you notice the function also contains the argument isTouch although I'm not sure what purpose that has. I tested on my iPhone through the simulator and it does indeed capture mousepressed as touches. So that's why I'm wondering which is best to use, whether mousepressed can capture multiple touches etc.

All I'm trying to do is capture touches for movement for a platform game so just left, right, jump and shoot basically. But even that would require up to three simultaneous touches. Your 'steep learning curve' tool may be overkill here.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Mouse or Touch function for mobile?

Post by raidho36 »

It's overkill if all you need to do is register a touch anywhere on the screen. For a normal graphical application with touch input, it's a very good fit. In any serious project, you either use pre-made "overkill" library or wind up making your own library of the same magnitude.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Mouse or Touch function for mobile?

Post by hasen »

Ok thanks, I'm sure it's a good library. I guess no one actually knows the answer to my question though unfortunately.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mouse or Touch function for mobile?

Post by zorg »

Raidho's first reply contained the answer to your questions though.

1. getTouches returns a list of currently active tactile inputs, i.e. your fingers currently touching the touch-screen.
2. touchPressed detects the "rising edge", i.e. when a touch starts; id-s are important if the screen is multi-touch capable.
3. however, if the screen can only handle one simultaneous touch, then, again as raidho said, for convenience's sake, Löve allows you to use the mouse callbacks for touch input as well.

If you need multiple simultaneous touch support, then i'd use the dedicated touch functions and callbacks.
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.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Mouse or Touch function for mobile?

Post by hasen »

zorg wrote: Sun Feb 25, 2018 2:33 pm Raidho's first reply contained the answer to your questions though.
Yes but I'm not sure if I want to use his library , especially since he said it was difficult. I was really trying to find out the difference between the mouse and touch specifically. Why do we need both mousepressed and touchpressed if they both detect touch? Since love.mousepressed also has an istouch argument.
zorg wrote: Sun Feb 25, 2018 2:33 pm 1. getTouches returns a list of currently active tactile inputs, i.e. your fingers currently touching the touch-screen.
2. touchPressed detects the "rising edge", i.e. when a touch starts; id-s are important if the screen is multi-touch capable.
3. however, if the screen can only handle one simultaneous touch, then, again as raidho said, for convenience's sake, Löve allows you to use the mouse callbacks for touch input as well.

If you need multiple simultaneous touch support, then i'd use the dedicated touch functions and callbacks.
Ok I see. getTouches seems like the one I need for holding down continuously for movement, Raidho mentioned it was like the isDown we use for keyboard and mouse presses although I don't see how you would do that with getTouches exactly. How would I go about doing this with getTouches:

Code: Select all

if love.mouse.isDown(1) then
   -- run left
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mouse or Touch function for mobile?

Post by zorg »

You'd use the value of the ID; if it just goes from 1 and counting up, then you could test for the touch that has an id of 1; if that vanishes, the touch ended.
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.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Mouse or Touch function for mobile?

Post by hasen »

zorg wrote: Sun Feb 25, 2018 4:53 pm You'd use the value of the ID; if it just goes from 1 and counting up, then you could test for the touch that has an id of 1; if that vanishes, the touch ended.
So like this:

Code: Select all

  local touches = love.touch.getTouches()
  for i, id in ipairs(touches) do
      local jx, jy = self.camera:toWorld(love.touch.getPosition(id))
	-- use :getPixel to get the colour of the button pressed?
  end
I assume the id is stored as 'id' in the table. ID is a table so it's hard to work with that. I was advised to detect the colour of the button by another on here in another thread - unless you have another way. What do you mean by the touch vanishes? How would you detect that?
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mouse or Touch function for mobile?

Post by zorg »

Quoting the wiki: "The id values are the same as those used as arguments to love.touchpressed, love.touchmoved, and love.touchreleased. The id value of a specific touch-press is only guaranteed to be unique for the duration of that touch-press. As soon as love.touchreleased is called using that id, it may be reused for a new touch-press via love.touchpressed."

I don't know what they meant by detect the color of the button; if we're talking about graphics, then i'd say that that'd be a waste of time; just check whether the x,y of a touch happened "inside" the button's graphic. to be honest, i think that'd also be possible with only using love.touchpressed...?
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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 43 guests