Corona SDK: touch event - sdk

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 )

Related

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

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.

Touch Hold Event In corona SDK

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".

How to detect two-finger tap using Corona SDK?

How can I detect when a tap is done with two fingers? The tap event can tell the number of taps, but not how many touches were involved in the event. Is there any way to figure it out? I have multitouch enabled in my Corona App. I have an application that simulates a left-button mouse click on a single-finger tap. And, a right-button mouse click on a double-finger tap.
EDIT:
To summarize and hopefully clarify, I want to:
Tap with my index finger once to emulate a left-button mouse click on my app. That is, 1 touch, 1 tap.
Tap with both my index and middle finger at the same time once to emulate a right-button mouse click on my app. That is, 2 touches at the same time, 1 tap.
Here's what Corona Staff had to say to my question in their forums:
http://forums.coronalabs.com/topic/35037-how-to-detect-two-finger-tap-in-corona
You can do it like this:
function object:tap( event )
if (event.numTaps >= 2 ) then
print( "The object was double-tapped." )
end
end
object:addEventListener( "tap" )
For more details regarding objects/screen taps in corona, see this...
Keep coding............ :)
Like Brent Sorrentino from Corona said: you should use multitouch.
First take a look at this http://docs.coronalabs.com/api/event/touch/id.html
You can already do it by yourself.
Here is my implementation:
system.activate( "multitouch" )
local object = display.newImage( "ball.png" )
object.numTouches = 0
function object:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( self, event.id )
self.numTouches = self.numTouches + 1
elseif event.phase == "cancelled" or event.phase == "ended" then
if self.numTouches <= 1 then
print( "This is a Left click" )
--call your onLeftClickFunction here
end
if self.numTouches == 2 then
print( "This is a Right click" )
--call your onRightClickFunction here
end
self.numTouches = 0
display.getCurrentStage():setFocus( nil )
end
return true
end
object:addEventListener( "touch", object )

how to do continous action on corona in tap function

How to do continuous action on corona in tap function? I mean when event.phase="began" and until its tapped the action repeats until its ended.
My code:
function upArrowtap(event)
if (event.phase == "began") then
if ( ball.y > 45 ) then
transition.cancel(trans1)
transition.cancel(trans2)
--ball.y = ball.y-15
start()
end
end
end
upArrow:addEventListener("touch", upArrowtap)
Hope u understand my question.
First off, use an event listener for "touch" not "tap". Tap event listeners only respond when the finger is removed, but touch listeners respond to both the beginning and end of the touch.
Secondly, to have an event repeat over and over you need to use enterFrame. So set an enterFrame listener when the touch begins and remove the enterFrame listener when the touch ends:
local function onEnterFrame(event)
ball.y = ball.y + 2
end
local function onTouch(event)
if (event.phase == "began") then
Runtime:addEventListener("enterFrame", onEnterFrame)
elseif (event.phase == "ended") then
Runtime:removeEventListener("enterFrame", onEnterFrame)
end
end
button:addEventListener("touch", onTouch)
(I may have gotten a couple keywords wrong, I just typed that off the top of my head)

Resources