Touchmoved function
Posted: Sun Oct 11, 2020 8:36 pm
Hello,
I've been trying to make a rectangle move on mobile devices using touch functions, but failed Can someone please explaing me:
1) how can I access values returned by callback functions, like touchmoved, or touchpressed - x, y, dx and dy? I tried to create new tables, and pass values from there into mine. I also tried to access that table directly, but get nil all the time.
2) Do i understand correctly, that if I get the exact value of finger movement (dx, dy) - I can later use it as dx and dy for the rectangle to move it on the screen?
My code is below, and I can't make the rectangle move, when I move my finger on the screen:
I've been trying to make a rectangle move on mobile devices using touch functions, but failed Can someone please explaing me:
1) how can I access values returned by callback functions, like touchmoved, or touchpressed - x, y, dx and dy? I tried to create new tables, and pass values from there into mine. I also tried to access that table directly, but get nil all the time.
2) Do i understand correctly, that if I get the exact value of finger movement (dx, dy) - I can later use it as dx and dy for the rectangle to move it on the screen?
My code is below, and I can't make the rectangle move, when I move my finger on the screen:
Code: Select all
function love.load()
font = love.graphics.newFont(10)
love.graphics.setFont(font)
end
touches = {}
rectX = 500
rectY = 200
rectDY = 0
function parseID(id)
return tostring(id)
end
function love.touchpressed(id, x, y, dx, dy)
touches[parseID(id)] = {id, x, y, dx, dy}
end
function love.touchmoved(id, x, y, dx, dy)
touches[parseID(id)] = {id, x, y, dx, dy}
end
function love.touchreleased(id, x, y, dx, dy)
touches[parseID(id)] = nil
end
function love.updated(dt)
if rectDY < 0 then
rectY = math.min(0, rectY + rectDY * dt)
else
rectY = math.max(rectY + rectDY * dt, 600)
end
end
function love.draw()
--debugInfo()
local id, x, y, dx, dy
for _, data in pairs(touches) do
if data then
id, x, y, dx, dy = unpack(data)
love.graphics.setLineWidth(5)
love.graphics.setColor(255,255,255)
rectDY = dy*1000
love.graphics.rectangle('fill', rectX, rectY, 10, 10)
end
end
end