how can i access x and y love.touch.getPosition variables?

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
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

how can i access x and y love.touch.getPosition variables?

Post 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
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: how can i access x and y love.touch.getPosition variables?

Post 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.
Rigachupe
Citizen
Posts: 83
Joined: Fri Jun 18, 2021 11:21 am

Re: how can i access x and y love.touch.getPosition variables?

Post by Rigachupe »

https://love2d.org/wiki/love

Read bottom page where touch functions are like love.touchpressed.
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

Re: how can i access x and y love.touch.getPosition variables?

Post 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
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: how can i access x and y love.touch.getPosition variables?

Post 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.
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

Re: how can i access x and y love.touch.getPosition variables?

Post 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
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: how can i access x and y love.touch.getPosition variables?

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

Who is online

Users browsing this forum: No registered users and 58 guests