Easy swipe on touchscreen android

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
9912
Prole
Posts: 8
Joined: Sun Aug 23, 2020 7:12 am

Easy swipe on touchscreen android

Post by 9912 »

Hello, i'm not very often here, but i want to share this code if someone wants an easy swipe up and down function

Code: Select all


function love.load()
	begin_y = nil
	end_y = nil
	doingTouch = false
end

function love.touchpressed(id, x, y, dx, dy, pressure)
	if doingTouch == false then
		begin_y = y
		doingTouch = true
	end
end

function love.touchmoved(id, x, y, dx, dy, pressure)
end

function love.touchreleased(id, x, y, dx, dy, pressure)
	end_y = y
	if doingTouch == true then
		if end_y < begin_y then
			--do what you want if you swiped [b]up[/b]
		elseif end_y > begin_y then
			--do what you want if you swiped [b]down[/b]
		elseif end_y == begin_y then
			--do what you want if you [b]touch[/b]
		end
		begin_y = nil
		end_y = nil
		doingTouch = false
	end
end

NOTE: feel free to add more features if you want, as of now it works perfectly on v11.3-Android, the best use of this code is with Talkies & Erogodic when you use choices in your visual novel ;)
User avatar
darkfrei
Party member
Posts: 1178
Joined: Sat Feb 08, 2020 11:09 pm

Re: Easy swipe on touchscreen android

Post by darkfrei »

9912 wrote: Mon May 24, 2021 6:17 am Hello, i'm not very often here, but i want to share this code if someone wants an easy swipe up and down function

Code: Select all


function love.load()
	begin_y = nil
	end_y = nil
	doingTouch = false
end

function love.touchpressed(id, x, y, dx, dy, pressure)
	if doingTouch == false then
		begin_y = y
		doingTouch = true
	end
end

function love.touchmoved(id, x, y, dx, dy, pressure)
end

function love.touchreleased(id, x, y, dx, dy, pressure)
	end_y = y
	if doingTouch == true then
		if end_y < begin_y then
			--do what you want if you swiped [b]up[/b]
		elseif end_y > begin_y then
			--do what you want if you swiped [b]down[/b]
		elseif end_y == begin_y then
			--do what you want if you [b]touch[/b]
		end
		begin_y = nil
		end_y = nil
		doingTouch = false
	end
end

NOTE: feel free to add more features if you want, as of now it works perfectly on v11.3-Android, the best use of this code is with Talkies & Erogodic when you use choices in your visual novel ;)
The code from https://love2d.org/wiki/love.graphics.translate works too:

Code: Select all

tx=0
ty=0
function love.draw()
	mx = love.mouse.getX()
	my = love.mouse.getY()
	if love.mouse.isDown(1) then
		if not mouse_pressed then
			mouse_pressed = true
			dx = tx-mx
			dy = ty-my
		else
			tx = mx+dx
			ty = my+dy
		end
	elseif mouse_pressed then
		mouse_pressed = false
	end
	love.graphics.translate(tx, ty)
	
	-- example graphics:
	love.graphics.circle( "line", 0, 0, 400 )
	love.graphics.line(-440, 0, 440, 0)
	love.graphics.line(0, -440, 0, 440)
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
9912
Prole
Posts: 8
Joined: Sun Aug 23, 2020 7:12 am

Re: Easy swipe on touchscreen android

Post by 9912 »

darkfrei wrote: Mon May 24, 2021 11:01 am The code from https://love2d.org/wiki/love.graphics.translate works too:

Code: Select all

tx=0
ty=0
function love.draw()
	mx = love.mouse.getX()
	my = love.mouse.getY()
	if love.mouse.isDown(1) then
		if not mouse_pressed then
			mouse_pressed = true
			dx = tx-mx
			dy = ty-my
		else
			tx = mx+dx
			ty = my+dy
		end
	elseif mouse_pressed then
		mouse_pressed = false
	end
	love.graphics.translate(tx, ty)
	
	-- example graphics:
	love.graphics.circle( "line", 0, 0, 400 )
	love.graphics.line(-440, 0, 440, 0)
	love.graphics.line(0, -440, 0, 440)
end
That works if you have a PC/Mac with mouse/track-pad, however the code has other purpose, i'll explain:
Some day ago, i was on a personal project making a visual novel, so i took erogodic with talkies as example, however it only works with physical keyboard, and therefore PC.
I'm targeting to use on Android which uses touchscreen, thus rendering the code unusable unless swipe gestures are implemented, so i've tested some codes around here and studying the touch functions from love.graphics library.
The code i've posted aims to be usable with smartphone touchscreen and its use cases includes -but not limited to- visual novels.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Easy swipe on touchscreen android

Post by Gunroar:Cannon() »

Nice :) . Seems like I'll be using it,
though you should have probally put this in the Library and Tools section(maybe as a tool...?) or General section, cause it seems you've got it figured out and don't need much help here, in ... that is... you know...Support and Development(though I guess it seems misleading, like "hey, come and post stuff to support others here" kind of thing :ultrahappy: )
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Easy swipe on touchscreen android

Post by zorg »

9912 wrote: Mon May 24, 2021 7:54 pm
darkfrei wrote: Mon May 24, 2021 11:01 am The code from https://love2d.org/wiki/love.graphics.translate works too:
...
That works if you have a PC/Mac with mouse/track-pad, however the code has other purpose, i'll explain:
Some day ago, i was on a personal project making a visual novel, so i took erogodic with talkies as example, however it only works with physical keyboard, and therefore PC.
I'm targeting to use on Android which uses touchscreen, thus rendering the code unusable unless swipe gestures are implemented, so i've tested some codes around here and studying the touch functions from love.graphics library.
The code i've posted aims to be usable with smartphone touchscreen and its use cases includes -but not limited to- visual novels.
While you're partially correct that the given code won't detect gestures, but the mouse functions can in fact be used as a 1-touch touch input as well, if you don't want to mess with multitouch (or if the device doesn't support it).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

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