user1597438
user1597438

Reputation: 2221

Restricting touch events in corona

I have a button on my class that takes the user from one scene to another like from Main Menu to the Game Page. Now, this works okay but I would like to restrict the touch. Like, if I touch the button then drag, then I the transition won't work but if I touch the button and let go it should work. How do I implement this? This is how my code currently looks but it's not working:

    if event.phase == "moved" then
    print("cannot be")
elseif event.phase == "began" then
    if event.phase == "ended" then
                storyboard.gotoScene("Game", "fade", 400)
    end
end

How can I restrict the touch event? Like if I touch the button, drag across the screen and end my touch on the button, it should not transition to the next scene?

Upvotes: 0

Views: 152

Answers (1)

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

You should try tap instead of touch. It is as follows:

 local function sceneChangeFunction()
     storyboard.gotoScene("Game", "fade", 400)
 end
 Runtime:addEventListener("tap",sceneChangeFunction)

or

if you want to use touch itself, then you can do it as below:

 local sceneChangeFlag = false  -- create a flag, make it false
 local function sceneChangeFunction(e)
     if(e.phase=="began")then
         sceneChangeFlag = true           -- make it true in touch began
     elseif(e.phase=="moved")then
         sceneChangeFlag = false          -- make it false in touch moved
     else
         if(sceneChangeFlag==true)then    -- scene changes only if flag==true 
             sceneChangeFlag = false
             storyboard.gotoScene("Game", "fade", 400)     
         end
     end
 end
 Runtime:addEventListener("touch",sceneChangeFunction)

Keep coding..... :)

Upvotes: 2

Related Questions