Button touch not responding

Hi everyone,

I have a problem with my code, were my button is not responding when i touch it.

I have the following in my create scene, i use composer :

local visuals = require("visualValues") -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen --create buttons shapeButton = widget.newButton( visuals.buttonOptionsShapes ) shapeButton.onEvent = handleGameButton colorButton = widget.newButton( visuals.buttonOptionsColors ) colorButton.onRelease = handleGameButton switchButton = widget.newButton( visuals.buttonOptionsSwitch ) switchButton.onRelease = handleGameButton print( shapeButton.onEvent, colorButton.onRelease, switchButton.onRelease ) print( shapeButton.isEnabled, colorButton.isEnabled, switchButton.isEnabled ) --add to sceneGroup sceneGroup:insert(shapeButton) sceneGroup:insert(colorButton) sceneGroup:insert(switchButton) end

and this is in the visualValues file:

--visualValues.lua local w, h = display.contentWidth, display.contentHeight -- here are all the variables related to visual and ui local visualValues = {} visualValues.bgImage = "space.jpg" -- background image visualValues.buttonOptions = { fontSize = 30, labelColor = { default={ 254/255, 185/255, 21/255 }, over={ 139/255, 23/255, 7/255 }}, labelAlign="center" } visualValues.buttonOptionsShapes = TableConcat({ x = w\*0.5, y = (2/6)\*h-10, id = "shapes", label = "Shapes" }, visualValues.buttonOptions) visualValues.buttonOptionsColors = TableConcat({ x = w\*0.5, y = (3/6)\*h, id = "colors", label = "Colors" }, visualValues.buttonOptions) visualValues.buttonOptionsSwitch = TableConcat({ x = w\*0.5, y = (4/6)\*h+10, id = "switch", label = "Switch" }, visualValues.buttonOptions) return visualValues

And this is the outcome of the code:

function: 0x600000849e10 function: 0x600000849e10 function: 0x600000849e10 nil nil nil

First i tried it with onEvent = handleGameButton in the visualValues file but that didn’t work.

I hope i explained my problem enough for you guys, and if you have any questions please ask.

Thanks for you time

Update:

This is the function they are directed to:

local function handleGameButton( event )

    --goto game screen

    print(“logging”)

    if event.phase == “ended” then

      composer.gotoScene( “GameScene”, { effect = “fade”, time = 400, params = { mode = event.target.id } } )

    end

    return true

end

local function handleGameButton( event ) --goto game screen print("logging") if event.phase == "ended" then composer.gotoScene( "GameScene", { effect = "fade", time = 400, params = { mode = event.target.id } } ) end return true end

But i never get any logging in the console if i touch the buttons.

And the print(switch button.isEnabled) was just for testing purpose. the same as the onRelease because onEvent wasn’t working.

Oké, another update i got it to work although it doesn’t look that nice.

I changed this:

shapeButton = widget.newButton( visuals.buttonOptionsShapes ) shapeButton.onEvent = handleGameButton

into this:

 shapeButton = widget.newButton( TableConcat(visuals.buttonOptionsShapes, {onEvent = handleGameButton}) )

this is the TableConcat function:

function TableConcat(t1,t2) for k, v in pairs(t2) do t1[k] = v end return t1 end

What do you have in the function handleGameButton?

(Does the background image appear?)

This section of code:

print( shapeButton.onEvent, colorButton.onRelease, switchButton.onRelease ) -- outcome: print( shapeButton.isEnabled, colorButton.isEnabled, switchButton.isEnabled )

is printing out this:

function: 0x600000849e10 function: 0x600000849e10 function: 0x600000849e10 nil nil nil

So it’s telling us that shapeButton.onEvent, colorButton.onRelease and switchButton.onRelease are all functions (and all using the same function as shown by the matching memory address).

It’s also telling us that shapeButton.isEnabled, colorButton.isEnabled and switchButton.isEnabled are all nil, which makes sense because I don’t see anywhere in your code where you could have set it.

I’m guessing that the handleGameButton function has some code along the lines of:

if shapeButton.isEnabled == true then doSomeStuff() end

and because isEnabled is never set to true when you create the object, it never triggers the if statement.

What do you have in the function handleGameButton?

(Does the background image appear?)

This section of code:

print( shapeButton.onEvent, colorButton.onRelease, switchButton.onRelease ) -- outcome: print( shapeButton.isEnabled, colorButton.isEnabled, switchButton.isEnabled )

is printing out this:

function: 0x600000849e10 function: 0x600000849e10 function: 0x600000849e10 nil nil nil

So it’s telling us that shapeButton.onEvent, colorButton.onRelease and switchButton.onRelease are all functions (and all using the same function as shown by the matching memory address).

It’s also telling us that shapeButton.isEnabled, colorButton.isEnabled and switchButton.isEnabled are all nil, which makes sense because I don’t see anywhere in your code where you could have set it.

I’m guessing that the handleGameButton function has some code along the lines of:

if shapeButton.isEnabled == true then doSomeStuff() end

and because isEnabled is never set to true when you create the object, it never triggers the if statement.