event listener not catching all button calls

I am having trouble getting all of my buttons recognized by an event listener. I have nine buttons each built out in a widget. There is an ‘onEvent’ declaration in the definition. When the code is run and I monitor the button id that is being picked up by the listener I get the following id’s when I click on the buttons in numeric order 1 - 9:

3,4,5,6,7,8,8,9 So where did 1 and 2 go? additionally if I use the png files for the default and over files the numbers picked up are even further off making no sense what so ever.

I am attaching a stub main and the test scene. If anyone can help I would appreciate it this is puzzling and frustrating.

local composer = require( "composer" ) composer.gotoScene( "scenes.mainTest")

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") --- -- manager classes --- --local mtrxMgr = require("classes.matrixMgr") --local coreMgr = require( "classes.coreMgr" ) --local scoreMgr = require( "classes.scoreMgr" ) ----- ---- utility ----- --local util = require("utilities.utility") --local global = require("utilities.globals") ----- ---- paths ----- --local menuPath = "images/menu/" --local colorPath = "images/colors/" --- -- vars, tables, groups --- w = display.contentWidth h = display.contentHeight --- -- start implementation --- --------------------------------------------------- -- boardListener -- waits for color placement by player -- checks for errors, locks cell on success --------------------------------------------------- --------------------------------------------------- -- selectInput -- controls the user input for menu actions -- exit, replay, pause --------------------------------------------------- local function selectListener(event) local target = event.target local buttonID = target.id local cmdButton = target.name local skipHint = false if ( event.phase == "began" ) then -- Get focus display.getCurrentStage():setFocus(event.target) print("buttonID: --------", buttonID) --util.print\_r(target) if( tonumber( buttonID ) ) then print("selectListener: ",buttonID) end elseif ( event.phase == "ended" ) then display.getCurrentStage():setFocus(nil) if( tonumber( buttonID ) ) then --allButtons[buttonID].alpha = 0 --coreMgr.soundCall() end end return true end -------------------------------------------------------------- -- onKeyEvent -------------------------------------------------------------- local function onKeyEvent(event) -- -- Get device and go to action menu on device back touch -- if ( event.keyName == "back" ) then local platformName = system.getInfo( "platformName" ) if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then return true end end end function scene:onBackKey(event) -- -- stop game, save and clear scene -- global.pauseGame = true coreMgr.stopTimer() coreMgr.processGameFile(0) coreMgr.stopGame(allButtons, gridButtons, menuButton) global.pauseGame = false -- remove all display objects parentGroup:removeSelf() music.stop(0) -- -- exit to main menu -- composer.removeScene("scenes.gameMgr") composer.showOverlay("scenes.mainMenu") composer.removeScene( "scenes.base", false ) composer.gotoScene("scenes.base") return true end -- end onKeyEvent --------------------------------------------------- -- main -- primary calls --------------------------------------------------- function scene:create( event ) local sceneGroup = self.view print("in create") --- -- local vars --- local buttonWidth = 30 local buttonHeight = 30 local startX = (w/2) - 165 local startYOdd = h - 135 local startYEven = h - 90 local offsetX = 25 --- -- begin display loop for user selection buttons --- --- -- Button images found in global.lua as arrays, will be in json file for final build -- Post touch buttons, full highlight --- local button1 = widget.newButton( { id = 1, x = (startX + (1-1)\* (buttonWidth + 5)) + offsetX, y = startYOdd + (1-1) \* buttonWidth, label = "1", onEvent = selectListener } ) sceneGroup:insert(button1) --relayout.add(button1) local button2 = widget.newButton( { id = 2, x = (startX + (2-1) \* (buttonWidth + 5)) + offsetX, y = startYEven + (1-1) \* buttonWidth, label = "2", onEvent = selectListener } ) sceneGroup:insert(button2) --relayout.add(button2) local button3 = widget.newButton( { id = 3, x = (startX + (3-1) \* (buttonWidth + 5)) + offsetX, y = startYOdd + (1-1) \* buttonWidth, label = "3", onEvent = selectListener } ) sceneGroup:insert(button3) --relayout.add(button3) local button4 = widget.newButton( { id = 4, x = (startX + (4-1) \* (buttonWidth + 5)) + offsetX, y = startYEven + (1-1) \* buttonWidth, label = "4", onEvent = selectListener } ) sceneGroup:insert(button4) --relayout.add(button4) local button5 = widget.newButton( { id = 5, x = (startX + (5-1) \* (buttonWidth + 5)) + offsetX, y = startYOdd + (1-1) \* buttonWidth, label = "5", onEvent = selectListener } ) sceneGroup:insert(button5) --relayout.add(button5) local button6 = widget.newButton( { id = 6, x = (startX + (6-1) \* (buttonWidth + 5)) + offsetX, y = startYEven + (1-1) \* buttonWidth, label = "6", onEvent = selectListener } ) sceneGroup:insert(button6) --relayout.add(button6) local button7 = widget.newButton( { id = 7, x = (startX + (7-1) \* (buttonWidth + 5)) + offsetX, y = startYOdd + (1-1) \* buttonWidth, label = "7", onEvent = selectListener } ) sceneGroup:insert(button7) --relayout.add(button7) local button8 = widget.newButton( { id = 8, x = (startX + (8-1) \* (buttonWidth + 5)) + offsetX, y = startYEven + (1-1) \* buttonWidth, label = "8", onEvent = selectListener } ) sceneGroup:insert(button8) --relayout.add(button8) local button9 = widget.newButton( { id = 9, x = (startX + (9-1) \* (buttonWidth + 5)) + offsetX, y = startYOdd + (1-1) \* buttonWidth, label = "9", onEvent = selectListener } ) sceneGroup:insert(button9) --relayout.add(button9) Runtime:addEventListener( "key", onKeyEvent ) end ------------------------------------------------------------- -- the rest of the composer stuff -------------------------------------------------------------- function scene:show(event) local sceneGroup = self.view if "will" == event.phase then print("show: ") ---controller(global.boardSize) --music.playStream(currentSong()) --math.random(#global.sounds - 1)) elseif "did" == event.phase then local lastScene = composer.getSceneName("previous") --print("show: lastScene: ", lastScene) if lastScene ~= nil then composer.removeScene(lastScene) end -- if global.musicSwitch then -- music.playStream("boardMusic", true) -- end --print("show: past music: ") -- if global.saveData[global.gameSub].sound then -- end --print("show: past sound: ") end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then end end function scene:destroy( event ) local sceneGroup = self.view --print("destroying game scene") end --------------------------------------------------------------------------- scene:addEventListener( "create" ) scene:addEventListener( "show" ) scene:addEventListener( "hide" ) scene:addEventListener( "destroy" ) ----------------------------------------------------------------------------------------- return scene

You are seeing that behaviour because you’ve barely given enough variables for the buttons. As you haven’t defined width for the buttons, I am sure that they are overlapping each other and since you haven’t given them an image or shape to define them, you can’t see this either because all that is visible is the number label at the centre of the button.

If you add width and height into the buttons, it’ll start working as intended. You should refer to the documentation for examples.

Also, when doing repetitive tasks like creating those buttons, you should use loops, e.g.

 

local button = {} for i = 1, 9 do button[i] = widget.newButton( { id = i, width = buttonWidth, height = buttonHeight, x = (startX + (i-1)\* (buttonWidth + 5)) + offsetX, y = startYOdd, label = i, onEvent = selectListener } ) sceneGroup:insert(button[i]) end

Thanks, silly oversight…

You are seeing that behaviour because you’ve barely given enough variables for the buttons. As you haven’t defined width for the buttons, I am sure that they are overlapping each other and since you haven’t given them an image or shape to define them, you can’t see this either because all that is visible is the number label at the centre of the button.

If you add width and height into the buttons, it’ll start working as intended. You should refer to the documentation for examples.

Also, when doing repetitive tasks like creating those buttons, you should use loops, e.g.

 

local button = {} for i = 1, 9 do button[i] = widget.newButton( { id = i, width = buttonWidth, height = buttonHeight, x = (startX + (i-1)\* (buttonWidth + 5)) + offsetX, y = startYOdd, label = i, onEvent = selectListener } ) sceneGroup:insert(button[i]) end

Thanks, silly oversight…