[Touch] Problems with setting focus on the object I want to activate

Hello, well I must say I’m kinda new with lua programming and sdk corona, so I need a clear and easy to understand explanation if possible, please.

The problem is I have to functions: background:touch and button:touch. The function background:touch draws circles in a layer0 wherever you touch the screen, meanwhile the function button:touch draws a rectangule in a corner of the screen in a different layer called layer 1.

When you touch the screen where the rectangule is, it should activate another function and that’s it. But when I touch the screen it activates the function AND draws a circle behind (in layer0).

I don’t want that. I want to draw circles in layer0 when I touch wherever the rectangule in layer1 is not, and when I “press” the rectangule it just activates the other function without drawing circles.

How can I do it? I’ve read a lot in the API references, an Corona Documentation, but unfortunately I’ve still got zero improvements.

Could you help me? Here is a little example of what I have in my code:

local background = display.newRect( capa0, 160,240,320,480) background:setFillColor(0) local buttonCalculo = display.newRect(capaSup, 280, 460, 80, 50) local txtbuttonCalculo = display.newText(capaSup, "Calcular", 280, 460) txtbuttonCalculo:setFillColor(0) function background:touch( event ) if(event.phase=="ended") then local circle = display.newCircle(layer0, event.x,event.y,30) end end background:addEventListener("touch", background) function buttonCalculo:touch( event2 ) if(event2.phase == began) then StageObject:setFocus(buttonCalculo) elseif(event2.phase == "ended") then print("Here I want to put the other function") end end buttonCalculo:addEventListener("touch", buttonCalculo)

Thank you very much in advance!!

To stop the touch events propagating through you need to add a ‘return true’ statement to each of your functions.

local background = display.newRect( capa0, 160,240,320,480) background:setFillColor(0) local buttonCalculo = display.newRect(capaSup, 280, 460, 80, 50) local txtbuttonCalculo = display.newText(capaSup, "Calcular", 280, 460) txtbuttonCalculo:setFillColor(0) function background:touch( event ) if(event.phase=="ended") then local circle = display.newCircle(layer0, event.x,event.y,30) end return true -- Add this line end background:addEventListener("touch", background) function buttonCalculo:touch( event2 ) if(event2.phase == began) then StageObject:setFocus(buttonCalculo) elseif(event2.phase == "ended") then print("Here I want to put the other function") return true -- Add this line end end buttonCalculo:addEventListener("touch", buttonCalculo)

It works! Thanks for your help ^^

To stop the touch events propagating through you need to add a ‘return true’ statement to each of your functions.

local background = display.newRect( capa0, 160,240,320,480) background:setFillColor(0) local buttonCalculo = display.newRect(capaSup, 280, 460, 80, 50) local txtbuttonCalculo = display.newText(capaSup, "Calcular", 280, 460) txtbuttonCalculo:setFillColor(0) function background:touch( event ) if(event.phase=="ended") then local circle = display.newCircle(layer0, event.x,event.y,30) end return true -- Add this line end background:addEventListener("touch", background) function buttonCalculo:touch( event2 ) if(event2.phase == began) then StageObject:setFocus(buttonCalculo) elseif(event2.phase == "ended") then print("Here I want to put the other function") return true -- Add this line end end buttonCalculo:addEventListener("touch", buttonCalculo)

It works! Thanks for your help ^^