prevent Tap / Touch conflict

Hi,
I’m writing an app with a menuBar which hides above the top screen fold and is is shown by a transition dragging it down.

I’d like to leave the user two options to hide / show the menu bar:

  1. by tapping the screen to toggle the menu on/off
  2. by dragging your finger up/down which causes the menu to drag up and down.

both functions work ok seperately, but when implemented together, it seems that the touch and tap events are both called together.

Is there a way to prevent a touch event from triggering when a tap event is called?

[import]uid: 33608 topic_id: 9763 reply_id: 309763[/import]

Anyone please? [import]uid: 33608 topic_id: 9763 reply_id: 36149[/import]

Just a guess of what might work…

When your tap event is called set a flag. Then you can check that in the touch event and if set, just return. Same with touch – set a flag and then check against that in tap.

Basically, either/both events may end up being called, but only the one that grabs it first will handle it – the other one will just return.

Jay
[import]uid: 9440 topic_id: 9763 reply_id: 36156[/import]

Thanks,
I tried that, but even with both flags, the menu does not respond properly to tap events.

Here is the code (without the new flags), if anyone has some insight about this problem, I’ll be very grateful.

First, the code for the touch event that allows the user to drag the menu up and down:

local turnMenuBarOn --used in function dragMenu, defined outside due to scope problem I couldnt solve when declared within the function  
local function dragMenu(event)  
 local t = event.target  
   
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus( t, event.id )  
 t.isFocus = true  
   
 -- Store initial position  
 t.y0 = event.y   
 elseif t.isFocus then  
 if "moved" == phase then  
 local originalMenuY  
  
 if menuBarOn == true then  
 originalMenuY = display.screenOriginY  
 else  
 originalMenuY = display.screenOriginY-70  
 end  
  
 --how many pixels to move the menu bar up or down  
 local moveAmount = event.y - t.y0   
  
  
 if moveAmount \> 0 then   
 turnMenuBarOn = true --at the end of the function use this variable to decide whether to transition the menu to the full on or full off position  
 else  
 turnMenuBarOn = false  
 end  
  
 --don't let the move amount be greater then 70 pixels  
 if moveAmount \> 70 then moveAmount = 70 end  
  
 --move the menu bar by the calculated amount  
 if (originalMenuY + moveAmount) \< display.screenOriginY then  
 menuBar.y = originalMenuY + moveAmount  
 end  
  
 menuBar.alpha = 1  
  
 elseif "ended" == phase or "cancelled" == phase then  
 --lock menu in full down position or full up position, depending on menuBarOn state  
 if turnMenuBarOn then   
 transition.to(menuBar,{time=300,alpha=1,y=display.screenOriginY})  
 menuBarOn = true  
 else  
 transition.to(menuBar,{time=300,alpha=0,y=display.screenOriginY-70})   
 menuBarOn = false  
 end  
  
 display.getCurrentStage():setFocus( t, nil )  
 t.isFocus = false  
 end  
 end  
 return true  
end  
mapArea:addEventListener("touch", dragMenu)  

Next, the code for the tap event which triggers a show/hide menu function:

local function showHideMenuBar()  
 if menuBarOn == false then transition.to(menuBar,{time=300,alpha=1,y=display.screenOriginY})  
 else transition.to(menuBar,{time=300,alpha=0,y=display.screenOriginY-70}) end  
 menuBarOn = not menuBarOn  
end --end showHideMenu()  
  
local function callShowHideMenuBar(e)  
 --we use this function so that we can call showHideMenu() from other areas in the code (not just from the tap events)  
 showHideMenuBar()  
return true  
end  
mapArea:addEventListener("tap", callShowHideMenuBar)  

Thanks again! [import]uid: 33608 topic_id: 9763 reply_id: 36184[/import]

isn’t “tap” just a really short touch with no “move”??

get rid of “tap” and just track it yourself in “touch” by calculating the coords (no movement) and that the time interval between began and ended phases is really short…

let us know if that works

[import]uid: 6175 topic_id: 9763 reply_id: 36190[/import]

according to me tap is equalto touch event with only “ended” phase no “began” or no “moved” phase

[lua]button1 = display.newRect(100,100,300,100)
button1.isFront = false

button2 = display.newRect(200,100,300,100)
button2.isFront = true
button2:setFillColor(255,0,0)

local simpleText = display.newText(“change”,0,0,native.systemFont,18)
simpleText.x = 200
simpleText.y = 250

local function changeNow()
if button1.isFront == false then
button1:toFront()
button2:toBack()
print(“white is on top”)
button2.isFront = false
button1.isFront = true
else
button2:toFront()
button1:toBack()
print(“red is on top”)
button1.isFront = false
button2.isFront = true
end
end
simpleText:addEventListener(“tap”,changeNow)

local myFlag = true

function button1Tap(e)
if myFlag == true then
print(“white only”)
myFlag = false
local function callMe()
myFlag = true
end
timer.performWithDelay(50,callMe)
end
end

function button2Tap()
if myFlag == true then
print(“red only”)
myFlag = false
local function callMe()
myFlag = true
end
timer.performWithDelay(50,callMe)
end
end
button2:addEventListener(“tap”,button2Tap)
button1:addEventListener(“tap”,button1Tap)[/lua] [import]uid: 12482 topic_id: 9763 reply_id: 36198[/import]

note : above example is working as both buttons are assigned with tap events if any one is with touch then always that will fire even another button is on top not tested so better to keep both for tap or both for touch

:slight_smile: [import]uid: 12482 topic_id: 9763 reply_id: 36199[/import]

Thanks. dgaedcke, your method works. I got rid of the tap function (callShowHideMenuBar) and instead call showHideMenuBar() when movement is 0.

I do have a new problem, that may or may not be related. Even though all my tap and touch functions return true, sometimes, when I click another tappable object on the screen, it will trigger the dragMenu() function, showing or hiding the menu. Not all the tappable objects above the mapArea do this, but some do. [import]uid: 33608 topic_id: 9763 reply_id: 36301[/import]