How to make a long touch on the button?

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! :slight_smile:

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.

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.