love.touchpressed doesnt work

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.
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

love.touchpressed doesnt work

Post by bigbruhh0 »

hello everyone,please tell me how to implement screen press check
I found this piece of code somewhere on the forum, but did not quite understand how it works:
it already works and reads screen clicks or is it just functions that I have to call directly in the update section
in this case, I do not understand how to call these functions

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

function howManyTouches()
  return #touches
end
i tried checking this way in update section,but touched always is equal to false:

Code: Select all

local zxc = howManyTouches()
if zxc>0
	then
		touched=true
	else
		touched=false
	end
im newbee in lua,so its hard to understand to me
before that I worked only on a game maker, I did not think about such things there xD
sorry for my bad english btw, its not my main language
Attachments
game.love
(7.54 KiB) Downloaded 132 times
in love with love2d
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.touchpressed doesnt work

Post by pgimeno »

bigbruhh0 wrote: Fri Mar 26, 2021 11:52 pm

Code: Select all

function howManyTouches()
  return #touches
end
Try this:

Code: Select all

function howManyTouches()
  local howMany = 0
  for k in pairs(touches) do
    howMany = howMany + 1
  end
  return howMany
end
#table is valid for tables that contain consecutive numeric indices starting with 1, and touch ids are not numeric.
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

Re: love.touchpressed doesnt work

Post by bigbruhh0 »

pgimeno wrote: Sat Mar 27, 2021 1:05 am Try this:
#table is valid for tables that contain consecutive numeric indices starting with 1, and touch ids are not numeric.
i changed to this, but is still doesnt working
where i should to place all of this functions?
in love with love2d
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

Re: love.touchpressed doesnt work

Post by bigbruhh0 »

oh,ok looks like I figured out how to do it, but there are still some problems with counting touches
in love with love2d
User avatar
darkfrei
Party member
Posts: 1173
Joined: Sat Feb 08, 2020 11:09 pm

Re: love.touchpressed doesnt work

Post by darkfrei »

Works fine
Clicker 10
Attachments
clicker-10.love
(1.27 KiB) Downloaded 162 times
Screenshot_20210327-111635.png
Screenshot_20210327-111635.png (72.27 KiB) Viewed 4552 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

Re: love.touchpressed doesnt work

Post by bigbruhh0 »

darkfrei wrote: Sat Mar 27, 2021 10:18 am Works fine
Clicker 10
wow thanks there is a lot of code here but i will try to study
can you please tell me , what am i doing wrong?
i just want to know this for future
out of despair, I already tried to write to the matrix by interpretation into string,but anyway i see message that index equal nil

Code: Select all

touches={}
function love.touchpressed(id, x, y)
  idd=tostring(id)
  touches[idd][1]=x
  touches[idd][2]=y
  touches[idd][3]=0
  touch_count=touch_count+1
end
function love.touchmoved(id, x, y)
  idd=tostring(id)
  touches[idd][1]=x
  touches[idd][2]=y
end
function love.touchreleased(id, x, y)
  idd=tostring(id)
  touches[idd]=nil

end
in love with love2d
User avatar
darkfrei
Party member
Posts: 1173
Joined: Sat Feb 08, 2020 11:09 pm

Re: love.touchpressed doesnt work

Post by darkfrei »

bigbruhh0 wrote: Sat Mar 27, 2021 3:27 pm wow thanks there is a lot of code here but i will try to study
can you please tell me , what am i doing wrong?
https://love2d.org/wiki/love.touch.getTouches

Code: Select all

function love.draw()
	local touches = love.touch.getTouches()
	for i, id in pairs(touches) do
		local x, y = love.touch.getPosition(id)
		love.graphics.circle("fill", x, y, 20)
		love.graphics.print('x: '..x..' y: '..y, x+20, y+20)
	end
end
https://love2d.org/wiki/love.touchpressed
The id is light userdata id https://love2d.org/wiki/light_userdata
Attachments
touch-01.love
(601 Bytes) Downloaded 148 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1173
Joined: Sat Feb 08, 2020 11:09 pm

Re: love.touchpressed doesnt work

Post by darkfrei »

So ids are userdata null and userdata 0x00()1
Attachments
Screenshot_20210327-165909.png
Screenshot_20210327-165909.png (46.22 KiB) Viewed 4516 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

Re: love.touchpressed doesnt work

Post by bigbruhh0 »

darkfrei wrote: Sat Mar 27, 2021 4:00 pm So ids are userdata null and userdata 0x00()1
thank you
can you answer one more question please?
in love.touchpressed i wrote this :

Code: Select all

touches_id[id]=0
and i want to draw this variable when im touching the screen :

Code: Select all

 for i, id in ipairs(touches) do
        local x, y = love.touch.getPosition(id)
        love.graphics.circle("fill", x, y, 20)
    love.graphics.print('x: '..x..' y: '..y, x+20, y-10)
    love.graphics.print('touch_id: '..touch_id[id], x, y+20)
    end
but i got error: attempt to index global 'touch_id' a nil value

as I understand, pressing the screen occurs after drawing, how can I fix this?

I will attach the file, but there is a lot of unnecessary code, everything you need is in main.lua
Attachments
game.love
(7.6 KiB) Downloaded 140 times
in love with love2d
MrFariator
Party member
Posts: 510
Joined: Wed Oct 05, 2016 11:53 am

Re: love.touchpressed doesnt work

Post by MrFariator »

In that code snippet, it seems you have written "touch_id" in place of "touches_id". "touch_id" is not defined, so it errors when you try to index it like a table.

Additionally, "ipairs" is intended for looping through tables with consuctive numeric indices, similar to the # operator. For this situation, you'd want to use "pairs", which loops through the contents of a table regardless of the type of indices involved, though the order is not guaranteed to be what you might expect.
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests