[Resolved] Button and screen touch at the same time.

Let me describe my problem.

My game has two actions, tap the left side of the screen for jump, tap right for rolling.

--what happens on touch  
local function touched( event )  
 --touch on the left, jump  
 if( event.x \< (\_W/2) ) then  
 --jumping code here...  
 end  
 --touch on the right, dash.  
 if ( event.x \> (\_W/2) ) then  
 --rolling code here...  
 end  
end  
  
--screen touch listener  
Runtime:addEventListener("touch", touched, -1);  

But there is also a pause button, located in the top right corner.

local function pause ()  
 if \_paused == false then  
 --stop actions  
 elseif \_paused == true then  
 --resume action  
 end  
end  
  
--pause listener  
pauseBtn:addEventListener("tap", pause, -1);  

My problem is that touching the pause button also activates the “rolling” action (because the button is on the right side of the screen) How can I make it so that doesn’t happen?

I guess I could just make a giant sensor behind the pause button and just listen for that, but would that affect performance? (something I’m really paranoid with).

[import]uid: 136402 topic_id: 29926 reply_id: 329926[/import]

[lua]local function pause ()
if _paused == false then
–stop actions
elseif _paused == true then
–resume action
end
return true
end

–pause listener
pauseBtn:addEventListener(“tap”, pause, -1);[/lua]

Adding return true to the pause function will stop the event from propagating to the runtime event listener. [import]uid: 147305 topic_id: 29926 reply_id: 119964[/import]

Thank you for your answer, but someone was too quick with the “resolved” tag, because that DID NOT solve the problem.

more often than not, the character starts his rolling animation before the game pauses.

[import]uid: 136402 topic_id: 29926 reply_id: 120080[/import]

What about something like (at the top of the ‘touched’ function):
[lua]if (isInside({event.x, event.y}, pauseButton.contentBounds)) then
return
end[/lua]
Where isInside(point, rect) checks if point is inside the given boundaries. [import]uid: 120659 topic_id: 29926 reply_id: 120108[/import]

Thanks for the idea! I managed to make this little function:

(inside the touched event function)

if (isInside(event.x, event.y, Ui[2].contentBounds)) then  
 return  
 end  

(where Ui[2] is the pause button)

function isInside(ex, ey, objBounds)  
 if ex \<= objBounds.xMax and ex \>= objBounds.xMin   
 and ey \<= objBounds.yMax and ey \>= objBounds.yMin then  
 return true  
 end  
 return false  
end  

So far seems to work ok. [import]uid: 136402 topic_id: 29926 reply_id: 120140[/import]

[lua]local function pause ()
if _paused == false then
–stop actions
elseif _paused == true then
–resume action
end
return true
end

–pause listener
pauseBtn:addEventListener(“tap”, pause, -1);[/lua]

Adding return true to the pause function will stop the event from propagating to the runtime event listener. [import]uid: 147305 topic_id: 29926 reply_id: 119964[/import]

Thank you for your answer, but someone was too quick with the “resolved” tag, because that DID NOT solve the problem.

more often than not, the character starts his rolling animation before the game pauses.

[import]uid: 136402 topic_id: 29926 reply_id: 120080[/import]

What about something like (at the top of the ‘touched’ function):
[lua]if (isInside({event.x, event.y}, pauseButton.contentBounds)) then
return
end[/lua]
Where isInside(point, rect) checks if point is inside the given boundaries. [import]uid: 120659 topic_id: 29926 reply_id: 120108[/import]

Thanks for the idea! I managed to make this little function:

(inside the touched event function)

if (isInside(event.x, event.y, Ui[2].contentBounds)) then  
 return  
 end  

(where Ui[2] is the pause button)

function isInside(ex, ey, objBounds)  
 if ex \<= objBounds.xMax and ex \>= objBounds.xMin   
 and ey \<= objBounds.yMax and ey \>= objBounds.yMin then  
 return true  
 end  
 return false  
end  

So far seems to work ok. [import]uid: 136402 topic_id: 29926 reply_id: 120140[/import]