Touch events - one runtime and one on object - actives in the same time

Hello,

I’m just beginning my adventure with Corona SDK and everything is going in the right direction.

I have a problem with touch events.

I have 3 elements in the scene:

  • button 1 (function: show ( hidden button ))

  • button 2 (function: any other)

  • hidden button (function: any other)

To top it all, after using button 1 , I would like to add touch event , which will be responsible for hiding a hidden button when player clicks anywhere on the screen.

Runtime:addEventListener("touch", hidde\_button)

Sample scenario:

  1. Player clicks button 1.

  2. Hidden button is shown.

  3. The player clicks somewhere nearby and hidden button is hidden.

It works, but the second scenario not:

  1. Player clicks button 2.

  2. Hidden button is shown.

  3. The player clicks on a button 2 , which calls his function.

Unfortunately, hidden button is still visible.

Can anyone give me a solution or some links that will help me?

Thank you.

Working example of problem in JQuery:

Like should be: http://jsfiddle.net/Sotery/beY5H/5/

Like is: http://jsfiddle.net/Sotery/2kywY/1/

can you post your code?

Ofc, here it is:

local widget = require( "widget" ) display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local button\_1 local button\_2 local hidden\_button local function hide\_hidden\_button (event) if event.phase == "ended" then Runtime:removeEventListener("touch", hide\_hidden\_button) hidden\_button.alpha = 0 end end local function show\_hidden\_button (event) hidden\_button.alpha = 1 Runtime:addEventListener("touch", hide\_hidden\_button) end button\_1 = widget.newButton { left = 0, top = 0, width = 100, height = 100, label = "Button 1", onRelease = show\_hidden\_button } local function function\_of\_button\_2 (event) print("button 2 function") end button\_2 = widget.newButton { left = 200, top = 0, width = 100, height = 100, label = "Button 2", onRelease = function\_of\_button\_2 } local function function\_of\_button\_3 (event) print("hidden button function") end hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = function\_of\_button\_3 } hidden\_button.alpha = 0

Change the function of button 3:

hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = hide\_hidden\_button }  

Well, i can do it, but in the code above function_of_button_3 is empty, because it is example code. In original i have different, big function. So it isnt a solution to my problem. I need to keep the  function_of_button_3.

this would be another solution:

local widget = require( "widget" ) display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local button\_1 local button\_2 local hidden\_button local function hide\_hidden\_button()     Runtime:removeEventListener("touch", runtimeTouchListener) hidden\_button.alpha = 0 end local function RuntimeListener (event)     if event.phase == "ended" then         hide\_hidden\_button()     end end local function show\_hidden\_button (event)     hidden\_button.alpha = 1     Runtime:addEventListener("touch", runtimeTouchListener) end button\_1 = widget.newButton { left = 0, top = 0, width = 100, height = 100, label = "Button 1", onRelease = show\_hidden\_button } local function function\_of\_button\_2 (event)     print("button 2 function") end button\_2 = widget.newButton { left = 200, top = 0, width = 100, height = 100, label = "Button 2", onRelease = function\_of\_button\_2 } local function function\_of\_button\_3 (event)     print("hidden button function") hide\_hidden\_button() end hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = function\_of\_button\_3 } hidden\_button.alpha = 0  

Read this for more info about touch events:

http://coronalabs.com/blog/2013/10/01/tutorial-taptouch-anatomy/

can you post your code?

Ofc, here it is:

local widget = require( "widget" ) display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local button\_1 local button\_2 local hidden\_button local function hide\_hidden\_button (event) if event.phase == "ended" then Runtime:removeEventListener("touch", hide\_hidden\_button) hidden\_button.alpha = 0 end end local function show\_hidden\_button (event) hidden\_button.alpha = 1 Runtime:addEventListener("touch", hide\_hidden\_button) end button\_1 = widget.newButton { left = 0, top = 0, width = 100, height = 100, label = "Button 1", onRelease = show\_hidden\_button } local function function\_of\_button\_2 (event) print("button 2 function") end button\_2 = widget.newButton { left = 200, top = 0, width = 100, height = 100, label = "Button 2", onRelease = function\_of\_button\_2 } local function function\_of\_button\_3 (event) print("hidden button function") end hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = function\_of\_button\_3 } hidden\_button.alpha = 0

Change the function of button 3:

hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = hide\_hidden\_button }  

Well, i can do it, but in the code above function_of_button_3 is empty, because it is example code. In original i have different, big function. So it isnt a solution to my problem. I need to keep the  function_of_button_3.

this would be another solution:

local widget = require( "widget" ) display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local button\_1 local button\_2 local hidden\_button local function hide\_hidden\_button()     Runtime:removeEventListener("touch", runtimeTouchListener) hidden\_button.alpha = 0 end local function RuntimeListener (event)     if event.phase == "ended" then         hide\_hidden\_button()     end end local function show\_hidden\_button (event)     hidden\_button.alpha = 1     Runtime:addEventListener("touch", runtimeTouchListener) end button\_1 = widget.newButton { left = 0, top = 0, width = 100, height = 100, label = "Button 1", onRelease = show\_hidden\_button } local function function\_of\_button\_2 (event)     print("button 2 function") end button\_2 = widget.newButton { left = 200, top = 0, width = 100, height = 100, label = "Button 2", onRelease = function\_of\_button\_2 } local function function\_of\_button\_3 (event)     print("hidden button function") hide\_hidden\_button() end hidden\_button = widget.newButton { left = 100, top = 200, width = 100, height = 100, label = "Hidden Button", onRelease = function\_of\_button\_3 } hidden\_button.alpha = 0  

Read this for more info about touch events:

http://coronalabs.com/blog/2013/10/01/tutorial-taptouch-anatomy/