Touch Hold Event In corona SDK - lua

I was wondering how I would check if the user has touched the screen, but is holding their touch down and is not moving. Please help if you have anything I can go from. Ive been looking around and have yet to find anything to handle this.

You can use/modify this: (It's what Rob Miracle says)
local holding = false
local function enterFrameListener()
if holding then
-- Holding button
-- Code here
-- Code here
-- Code here
else
-- Not holding
-- Code here
-- Code here
-- Code here
end
end
local function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
I believe its obvious what touchHandler function is ^^

You will need a touch listener added to either an object covering your whole screen or you can add it to the system's RunTime.
See this guide: http://docs.coronalabs.com/guide/events/detectEvents/index.html#hit
Now, there are three "Phases" for these touch events. You get one when the press starts ("began"), one if the person moves their finger ("moved") and when they stop touching, there is an "ended" phase.
Depending on what you are trying to do, if you are say moving something while holding the button down, then you can set a flag like:
if event.phase == "began" then
pressed = true
elseif event.phase == "ended" then
pressed = false
end
Then where ever you're moving you can check to see "if pressed then move".

Related

Corona SDK While loop crash

What is wrong with this code that it crashes the simulator?
I'm new to Corona SDK, but I know alot of Lua from Roblox.
local x,y,touching,active = 2,2,false,true
local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,y)
background:scale(60,60)
print("Started")
function move()
y=y+1
end
function onObjectTouch(event)
if event.phase == "began" then
touching = true
while touching == true do
timer.performWithDelay( 1000, move)
end
elseif event.phase == "ended" then
touching = false
end
return true
end
background:addEventListener("touch",onObjectTouch)
Corona is event based. You essentially lock up the touch event in a while loop which prevents the ended phase from triggering to break your endless loop.
Corona has a Runtime event called "enterframe". It is called 30-60 times a second (probably less depending on the work it is doing) that is essentially an update method.
Using your logic:
function onObjectTouch(event)
if event.phase == "began" then
touching = true
elseif event.phase == "ended" then
touching = false
end
end
funtion EnterFrame(event)
if touching == true then
timer.performWithDelay( 1000, move)
end
end
There are other ways to accomplish this by setting a dynamic objects velocity when the touch is started and setting that velocity to 0 when the touch ends.

Non stopping movement

I've been having some trouble with player movement. The thing is, movement is controlled by four arrow-buttons and while one of them is pressed, the character moves in that direction. However, if the player doesn't lift his finger before moving away from the button, the player moves indefinitely. The code is the following:
function moveright(self,event)
self.x=self.x+2
end
function rightkeypressed(event)
if event.phase == "began" then
knight.enterFrame = moveright
Runtime:addEventListener("enterFrame",knight)
end
if event.phase == "ended" or event.phase == "cancelled" then
Runtime:removeEventListener("enterFrame",knight)
end
end
rightkey:addEventListener("touch",rightkeypressed)
This is the same for all four directions.
Very Simple once you understand the touch listeners
if event.phase == "ended" or event.phase == "cancelled" then
knight.enterFrame = moveright
Runtime:addEventListener("enterFrame",knight)
Runtime:removeEventListener("enterFrame",knight)
end
That should work if you have any questions or is not working just ask

In Corona SDK, how can I put a restart function at the end of my game?

I have already made a restart button, but I don't know how to attach a restart function to the button. If anyone could help, that would be great.
local widget = require("widget")
local function Restart(event)
if "began" == event.phase then
--code here when touch begin
elseif "moved" == event.phase then
--code here when move
elseif "ended" == event.phase or "cancelled" == event.phase then
--code here when touch end
end
end
-- Create the widget
local button1 = widget.newButton{
left = 100,
top = 200,
id = "button1",
label = "Label",
onEvent = Restart
}
http://docs.coronalabs.com/api/library/widget/newButton.html
You can have a look at it.
By the way, when restart your game don't forget init Variables & display.* (like display.newGroup() / display.newImage() etc.)

How do I make a collison while someone is holding a button

So I have this cube which if the player clicks on a button moves. I also have this block which if the cube collides with, the cube will get sent back to a start position. I have tried my best but I can't seem to get both of them working correctly together. With my code, the cube get's glitchy and moves all over the screen between the block and start position. This is if the button is still pressed. If it is released at the same moment of collision it does work, but obviously the players won't be paying attention to that.
function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
leftbutton:addEventListener( "touch", touchHandler )
This is the code for the collision:
function onCollision( event )
if ( event.phase == "began" ) then
transition.cancel( )
transition.moveTo( cube, {time = 0, x = 35, y = 35} )
end
return true
end
redblock:addEventListener( "collision", onCollision )
Also: whenever the cube falls on an edge of the redblock and starts spinning and gets sent back to start. It keeps spinning and it starts moving on its own.
I hope someone can help!
Thanks.
You are still in the "moved" phase during the collision hence why the block tries to reset, but is then dragged around afterwards. Once you detect the collision, you need to remove the event handler from the "leftbutton" so that the touch event is no longer in progress.

Corona SDK: touch event

How can I detect if screen is being touched? Seems to me, touch events are not generated while screen is touched and the finger is not moving.
Yes, only changes to the finger movement is recorded. Put down finger, lift up finger and dragging around triggers event.
However, you could do
e.phase == "began"
in your event function. That would be triggered when the user puts his finger on the screen.
Touch events are handled in phases. So the event being generated by the touch has "began", "moved", "ended" and "cancelled" phases. You could use detection, therefore, by doing this:
self.isTouched = false;
function defaultTouchHandler(e)
if(e.phase == "began") then
print("Tapped")
self.isTouched = true;
--User has touched the screen (not moving). Do "onMouseDown" things here
elseif(e.phase == "moved") then
print("Moved")
--User is moving their finger wile touching. Do "onMouseMoved" things here
elseif(e.phase == "cancelled" or e.phase == "ended") then
print("End of touch")
self.isTouched = false;
--User lifted their finger, or an interrupt happened. Do "onMouseUp" things here
end
end
self:addEventListener("touch", defaultTouchHandler)
When you then need to check if the screen is being touched, simply do:
if(isTouched) then
--Screen is being touched
else
--Screen is not being touched
end
EDIT: Obviously you can change "self" on the addEventListener line to be any object you wish to listen for touch events on
local object = display.newImage( "ball.png" )
object.id = "ball object"
local function onObjectTouch( event )
if ( event.phase == "began" ) then
print( "Touch event began on: " .. event.target.id )
elseif ( event.phase == "ended" ) then
print( "Touch event ended on: " .. event.target.id )
end
return true
end
object:addEventListener( "touch", onObjectTouch )

Resources