Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to make a long touch on the button?
Started by Artemako Jan 16 2019 09:36 AM

1 reply to this topic
button long touch

Best Answer XeduR @Spyric , 17 January 2019 - 04:31 AM

There are a few ways of approaching this. One would be to use an enterFrame function that checks if a button is being pressed or not. Then all your buttons would need to do is to set a variable to be true or false whether they are being pressed or not, like:

 

local dot = display.newCircle( display.contentCenterX, display.contentCenterY-40, 20 )
local moveRight = false

local function move(event)
	if moveRight == true then
		dot.x = dot.x+1
	end
end
Runtime:addEventListener( "enterFrame", move )

local function buttonFunction(event)
	if event.phase == "began" or event.phase == "moved" then
		if event.target.id == "moveRight" then
			moveRight = true
		end
	elseif event.phase == "ended" or event.phase == "cancelled" then
		moveRight = false
	end
	return true
end

local buttonRight = display.newRect( display.contentCenterX, display.contentCenterY+40, 60, 60 )
buttonRight.id = "moveRight"
buttonRight:addEventListener( "touch", buttonFunction )

Note that this is a bit rudimentary example and you'd have to write some rules for cancelling the touch in case the user slides their finger off the button, etc. otherwise the touch will just keep going. But, this should probably give you some ideas on how to approach your problem.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Artemako

[GLOBAL: userInfoPane.html]
Artemako
  • Observer

  • 4 posts
  • Corona SDK

Hey.
Help how to make a long touch on the button for smartphones.
I have the buttons left and right and they are responsible for the movement. But they work when I press and hold one of them.
I read in LEARN, but I did not find.
Thank you in advance! :)


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 616 posts
  • Corona SDK

  Best Answer

There are a few ways of approaching this. One would be to use an enterFrame function that checks if a button is being pressed or not. Then all your buttons would need to do is to set a variable to be true or false whether they are being pressed or not, like:

 

local dot = display.newCircle( display.contentCenterX, display.contentCenterY-40, 20 )
local moveRight = false

local function move(event)
	if moveRight == true then
		dot.x = dot.x+1
	end
end
Runtime:addEventListener( "enterFrame", move )

local function buttonFunction(event)
	if event.phase == "began" or event.phase == "moved" then
		if event.target.id == "moveRight" then
			moveRight = true
		end
	elseif event.phase == "ended" or event.phase == "cancelled" then
		moveRight = false
	end
	return true
end

local buttonRight = display.newRect( display.contentCenterX, display.contentCenterY+40, 60, 60 )
buttonRight.id = "moveRight"
buttonRight:addEventListener( "touch", buttonFunction )

Note that this is a bit rudimentary example and you'd have to write some rules for cancelling the touch in case the user slides their finger off the button, etc. otherwise the touch will just keep going. But, this should probably give you some ideas on how to approach your problem.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: button, long, touch