Page 1 of 1
how can i access x and y love.touch.getPosition variables?
Posted: Wed May 24, 2023 5:37 pm
by Yani354
i have a problem that i don't know how to fix and don't know if there is a way to fix it.
is there a way that i can access the x and y that are in the for loop.
This is just an easy example:
Code: Select all
function love.load()
touches = love.touch.getTouches()
for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
varX = x
varY = y
end
end
function love.update(dt)
end
function love.draw()
love.graphics.print(varX)
love.graphics.print(varY,0,20)
end
Re: how can i access x and y love.touch.getPosition variables?
Posted: Wed May 24, 2023 6:41 pm
by Andlac028
You are checking only for touches, that are present during the game load (function love.load), I think, you wanted to use it in love.update, which is below and empty.
Re: how can i access x and y love.touch.getPosition variables?
Posted: Wed May 24, 2023 7:39 pm
by Rigachupe
https://love2d.org/wiki/love
Read bottom page where touch functions are like love.touchpressed.
Re: how can i access x and y love.touch.getPosition variables?
Posted: Wed May 24, 2023 11:02 pm
by Yani354
Andlac028 wrote: ↑Wed May 24, 2023 6:41 pm
You are checking only for touches, that are present during the game load (function love.load), I think, you wanted to use it in love.update, which is below and empty.
Sorry i wasn't very clear with my code this is a better example:
Code: Select all
Joystick = {}
function Joystick:load()
touches = love.touch.getTouches()
--how can i access them globaly
for i, id in ipairs(touches) do
x, y = love.touch.getPosition(id)
self.ConMouseX = x
self.ConMouseY = y
end
self.aRadius = 25
self.bRadius = 75
self.distance = 0
self.angle = 0
self.aX = 0
self.aY = 0
self.isDown = false
end
function Joystick:update(dt)
end
function love.touchpressed(id, x, y, dx, dy, pressure)
--PROBLEM bx and by are nil
Joystick.bX = Joystick.ConMouseX
Joystick.bY = Joystick.ConMouseY
Joystick:Following(x,y)
Joystick.isDown = true
end
function love.touchreleased ( id, x, y, dx, dy, pressure )
Joystick.isDown = false
Joystick.ConMouseX = x
Joystick.ConMouseY = y
Joystick.distanceX = 0
Joystick.distanceY = 0
Joystick.distance = 0
Joystick.angle = 0
Joystick.aX = 0
Joystick.aY = 0
end
function Joystick:Following(x,y)
--PROBLEM distanceX and distanceY are nil
self.distanceX = x - self.bX
self.distanceY = y - self.bY
self.distance = math.min(math.sqrt(self.distanceX^2 + self.distanceY^2), self.bRadius)
self.angle = math.atan2(self.distanceY,self.distanceX)
self.aX = self.bX + (math.cos(self.angle) * self.distance)
self.aY = self.bY + (math.sin(self.angle) * self.distance)
end
function Joystick:draw()
if self.isDown then
love.graphics.circle("fill", self.aX, self.aY,self.aRadius)
love.graphics.circle("line",self.bX, self.bY,self.bRadius)
end
end
Re: how can i access x and y love.touch.getPosition variables?
Posted: Thu May 25, 2023 4:23 am
by Andlac028
You set bx to ConMouseX, which you initialize in load as latest active touch, which almost always would be none, so the loop is not called at all and ConMouseX is not set to any number, so it is nil.
Re: how can i access x and y love.touch.getPosition variables?
Posted: Thu May 25, 2023 9:24 am
by Yani354
Andlac028 wrote: ↑Thu May 25, 2023 4:23 am
You set bx to ConMouseX, which you initialize in load as latest active touch, which almost always would be none, so the loop is not called at all and ConMouseX is not set to any number, so it is nil.
i know that ConMouseX is nil i am asking if there is a way that i can initialize ConMouseX in load function and use it elsewhere
that is an mouse example
Code: Select all
function love.load()
MouseX, MouseY = love.mouse.getPosition()
ConMouseX = MouseX
ConMouseY = MouseY
end
function love.update(dt)
MouseX, MouseY = love.mouse.getPosition()
end
function love.mousepressed(x, y, button, istouch)
if button == 1 then
ConMouseX = MouseX
ConMouseY = MouseY
end
end
function love.draw()
love.graphics.print(table.concat({
'MouseX: '..tostring(MouseX),
'MouseY: '..tostring(MouseY),
'ConMouseX: '..tostring(ConMouseX),
'ConMouseY: '..tostring(ConMouseY),
}, '\n'))end
is there a way that i can do that with love.touch
Re: how can i access x and y love.touch.getPosition variables?
Posted: Thu May 25, 2023 11:54 am
by Andlac028
Mouse always have some position, touch can appear and disappear. Just initialize it to some position, like center of the windows. What else you want to initialize it to?