1 "tap" - 2 event listeners

Hello there!

There is 2 addEventListener() in my code:

Runtime:addEventListener("tap",tap\_on\_scrn) btn\_restart:addEventListener("tap",btn\_restart\_pressed)

The problem is, when I’m “pressing”  btn_restart my program is calling 2 functions:tap_on_scrn() AND btn_restart_pressed(). But i want to call ONLY btn_restart_pressed().

How to do that?

In the code: Runtime: addEventListener (“tap”, tap_on_scrn) you call a function when the user touches the screen, don’t you? if true, you could add an event to the object on the screen itself, why put a tap event on the Runtime?

jdsmedeirosbr, you mean that screen is object too?

I don’t know which background you use in your application, whether it’s an image or just a colored rectangle, but whatever it is, you can create a rectangle with the size of the screen and use it with a touch event.

I got this, thanks!

Also remember to have your listener function “return true” if you want the touch to stop propagating to objects under the object that was touched or tapped.

[lua]

local function myTouchListener( event )

    if ( event.phase == “began” ) then

        – Code executed when the button is touched

    elseif ( event.phase == “moved” ) then

        – Code executed when the touch is moved over the object

    elseif ( event.phase == “ended” ) then

        – Code executed when the touch lifts off the object

    end

    return true  – Prevents tap/touch propagation to underlying objects

end

local myButton = display.newRect( 100, 100, 200, 50 )

myButton:addEventListener( “touch”, myTouchListener )  – Add a “touch” listener to the object

[/lua]

In the code: Runtime: addEventListener (“tap”, tap_on_scrn) you call a function when the user touches the screen, don’t you? if true, you could add an event to the object on the screen itself, why put a tap event on the Runtime?

jdsmedeirosbr, you mean that screen is object too?

I don’t know which background you use in your application, whether it’s an image or just a colored rectangle, but whatever it is, you can create a rectangle with the size of the screen and use it with a touch event.

I got this, thanks!

Also remember to have your listener function “return true” if you want the touch to stop propagating to objects under the object that was touched or tapped.

[lua]

local function myTouchListener( event )

    if ( event.phase == “began” ) then

        – Code executed when the button is touched

    elseif ( event.phase == “moved” ) then

        – Code executed when the touch is moved over the object

    elseif ( event.phase == “ended” ) then

        – Code executed when the touch lifts off the object

    end

    return true  – Prevents tap/touch propagation to underlying objects

end

local myButton = display.newRect( 100, 100, 200, 50 )

myButton:addEventListener( “touch”, myTouchListener )  – Add a “touch” listener to the object

[/lua]