Button listeners do not get focus

I have an app with five buttons on a main screen. I have  created five onTouch functions, one for each button. Here is one example:

– Touch event listener for Settings button

local onSettingsTouch = function( event )

print(“Touch:Settings event=”, event.phase)

    if event.phase == “began” then

        storyboard.gotoScene( “settings”, “slideLeft”, 800  )

        return true

    end

end

buttonSettings = widget.newButton

{

    id = “buttonSettings”,

    label = “Settings”,

    defaultFile = “buttonWhite.png”,

    overFile = “buttonWhiteOver.png”,

    emboss = true,

    isEnabled   = true,

    onPress = onSettingsTouch,

    onRelease = onSettingsTouch,

    onEvent  = onSettingsTouch,

}

buttonSettings.touch = onSettingsTouch

group:insert(buttonSettings)

function scene:enterScene( event )

    

    print( "Entry: enterScene event start: ", event )

    buttonSettings:addEventListener( “touch”, buttonSettings )

    print( "Entry: enterScene event end: ", event )

    

end

Everything displays correctly, but I never get to the onSettingsTouch function when I click the Settongs button. The print messages from the enterScene function display, but not the one in the onSettingsTouch function. I tried linking the onSettingsTouch function to my background display object and it worked fine.

So how do I get the button to connect to the onSettingsTouch function? I know I am missing something obvious, but what?

Thanks!!

You should not have onPress and onRelease if you have onEvent configured. (onPress and onRelease have no phase either)

Also there’s no need to add your own touch listeners. The widget will take care of that for you.

If you modify your code as follows and remove the lines I’ve commented out (lines 19, 20, 23 and 30),  it should work:

-- Touch event listener for Settings button local onSettingsTouch = function( event ) print("Touch:Settings event=", event.phase) if event.phase == "began" then storyboard.gotoScene( "settings", "slideLeft", 800 ) return true end end ... ... buttonSettings = widget.newButton { id = "buttonSettings", label = "Settings", defaultFile = "buttonWhite.png", overFile = "buttonWhiteOver.png", emboss = true, isEnabled = true, --(remove this) onPress = onSettingsTouch, --(remove this) onRelease = onSettingsTouch, onEvent = onSettingsTouch, } --(remove this) buttonSettings.touch = onSettingsTouch group:insert(buttonSettings) ... ... function scene:enterScene( event ) print( "Entry: enterScene event start: ", event ) --(remove this) buttonSettings:addEventListener( "touch", buttonSettings ) print( "Entry: enterScene event end: ", event ) end

RESOLVED:

Thanks, Ingemar, that got it working!

Just as as FYI, the onPress and onRelease functions were added to the button definition as a last gasp effort to get it to work. The other code that needed to be deleted came from the StoryBoards example code.

I spent about 5 hours reading API documentation and tutorials. Everything I read matched the code that I had. Is there some documentation that points out how this is done for a button?

Again, many thanks.

Joe

Have you checked out Corona University?

http://www.coronalabs.com/resources/tutorials/user-interface-scenes-and-widgets/

There are quite a few useful tips there…

A button widget is being hidden by some images and not others.  How to adjust the “visibility layer” of a widget?

Hi @bigchase,

Do you mean the z-index of the widget? You need to bring the widget to the “front” using “widget:toFront()”, or put the widget in a display group that resides in front of all other groups.

Please read the guide on display objects and groups, if you haven’t already:

http://docs.coronalabs.com/guide/start/displayGroups/index.html

Best regards,

Brent

You should not have onPress and onRelease if you have onEvent configured. (onPress and onRelease have no phase either)

Also there’s no need to add your own touch listeners. The widget will take care of that for you.

If you modify your code as follows and remove the lines I’ve commented out (lines 19, 20, 23 and 30),  it should work:

-- Touch event listener for Settings button local onSettingsTouch = function( event ) print("Touch:Settings event=", event.phase) if event.phase == "began" then storyboard.gotoScene( "settings", "slideLeft", 800 ) return true end end ... ... buttonSettings = widget.newButton { id = "buttonSettings", label = "Settings", defaultFile = "buttonWhite.png", overFile = "buttonWhiteOver.png", emboss = true, isEnabled = true, --(remove this) onPress = onSettingsTouch, --(remove this) onRelease = onSettingsTouch, onEvent = onSettingsTouch, } --(remove this) buttonSettings.touch = onSettingsTouch group:insert(buttonSettings) ... ... function scene:enterScene( event ) print( "Entry: enterScene event start: ", event ) --(remove this) buttonSettings:addEventListener( "touch", buttonSettings ) print( "Entry: enterScene event end: ", event ) end

RESOLVED:

Thanks, Ingemar, that got it working!

Just as as FYI, the onPress and onRelease functions were added to the button definition as a last gasp effort to get it to work. The other code that needed to be deleted came from the StoryBoards example code.

I spent about 5 hours reading API documentation and tutorials. Everything I read matched the code that I had. Is there some documentation that points out how this is done for a button?

Again, many thanks.

Joe

Have you checked out Corona University?

http://www.coronalabs.com/resources/tutorials/user-interface-scenes-and-widgets/

There are quite a few useful tips there…

A button widget is being hidden by some images and not others.  How to adjust the “visibility layer” of a widget?

Hi @bigchase,

Do you mean the z-index of the widget? You need to bring the widget to the “front” using “widget:toFront()”, or put the widget in a display group that resides in front of all other groups.

Please read the guide on display objects and groups, if you haven’t already:

http://docs.coronalabs.com/guide/start/displayGroups/index.html

Best regards,

Brent