Help with android touchpad using touchscreen

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
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Help with android touchpad using touchscreen

Post by kicknbritt »

:shock: Hello there lovely freinds, Boy do I have some ugly code for you today! So im in a car driving through chicago with my aunt, and i decided to take a stab at writing a touch operated directional pad to move my player in my game... i am absolutely dumbfounded at the way i can implement this... Basically ive tried to assaign love.gettouches x and y values to a global variable and then check if that global variable is within the arrow pads and then move char... etc. But whenever i touch a directional pad and lift my finger, The control pad stays pressed! :x all I need it to do is stop moving when i lift my finger DX. Any help would be much appreciated.
Oh and heres the code.

Code: Select all

function ScreenTouches()

	local touches = love.touchpressed(  )
released = love.touchreleased( )
	for i, id in ipairs(touches) do 
		 tchx, tchy = love.touch.getPosition(id)
	

	end


end


function Touchpad()


if tchx > rx and tchx < rx+rightarrow:getWidth() and tchy > ry and  tchy < ry+rightarrow:getHeight() then

player.x = player.x + player.speed * delt
end


if tchx > lx and tchx < lx+leftarrow:getWidth() and tchy > ly and  tchy < ly+leftarrow:getHeight() then

player.x = player.x - player.speed * delt
end


if tchx > dx and tchx < dx+downarrow:getWidth() and tchy > dy and  tchy < dy+downarrow:getHeight() then

player.y = player.y + player.speed * delt
end


if tchx > ux and tchx < ux+uparrow:getWidth() and tchy > uy and  tchy < uy+uparrow:getHeight() then

player.y = player.y - player.speed * delt


end


end
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Help with android touchpad using touchscreen

Post by Positive07 »

love.touchpressed and love.touchreleased are callbacks, not functions, you define them and LÖVE calls them when a touch is pressed or released respectively.

There is also love.touch.getTouches returns a table with touches, which you can then pass to love.touch.getPosition to get their x, y coordinates.

This however won't tell you if a new finger touched the screen or if an existing touch was released. So you can define love.touchpressed and save the id in a table if it's inside the pad, and remove the id when that same id is passed to love.touchreleased, there is also love.touchmoved that is called when a touch changed it's coordinate.

Also you are only saving one tchx and tchy so you if I have 3 fingers in screen you will only handle one

So you should change how ScreenTouches works
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Help with android touchpad using touchscreen

Post by kicknbritt »

Okay.. thanks. So im going to define touchpressed and then store, the id in a table, then remove the id with touch released... gimme a sec ill try that.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Help with android touchpad using touchscreen

Post by kicknbritt »

Im sorry im having a hard time with this... :?
Ive been trying at it for around 30 mins and sorry im not trying to being a leech but can you give an example of a better way i can write the screentouches function? This is what i got.

Code: Select all

function ScreenTouches()
table = love.touch.getTouches()
touches = love.touchpressed(  )
released = love.touchreleased( )

	for i, id in ipairs(table) do 
		 x, y = love.touch.getPosition(id)
	

if x > rx and x < rx+rightarrow:getWidth() and y > ry and  y < ry+rightarrow:getHeight() then

player.x = player.x + player.speed * delt----the right arrow
end

	end


end
 
Not sure how to add/remove the id using touch pressed and released :/. Thanks for your patience.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Help with android touchpad using touchscreen

Post by Positive07 »

This is not how you define a callback, here you are calling touchreleased and storing the returned value in the released variable, but since love.touchreleased is not defined it will most likely rise an error "trying to call love.touchreleased, a nil value"

Code: Select all

released = love.touchreleased( )
In lua you define callback functions like so:

Code: Select all

function love.touchreleased ( id, x, y, dx, dy, pressure )
   --Do stuff when a touch is released
end
If you don't know Lua I recommend you learn that first, using the PiL or the Manual, also be sure to check the LÖVE wiki in order to know what each function does, the love page has information in the different modules and the callbacks. There is also this great serie of tutorials by Sheepolution that is worth checking out
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Help with android touchpad using touchscreen

Post by kicknbritt »

Oh. Well thanks ill try to touch up on my lua... D: i never had any actual classes so my knowlege is a bit.. incomplete but thanks for the help.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Help with android touchpad using touchscreen

Post by Positive07 »

No problem! And don't worry you can always ask questions here if you have trouble... We have all been there
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Help with android touchpad using touchscreen

Post by kicknbritt »

Thanks... this forum is very Lovely!
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
Post Reply

Who is online

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