Button Tapped error?

My buttonTapped Function is working fine but my buttonTapped1 function isn’t working at all…

my button is usually connected to a table thats they have sceneName that is equal to the directory. but this error always pops up… addition button is fine in my UI but every time i click Subtraction it has an error.

operationMenu.lua local touchEnabled = false local buttonTapped local buttonTapped1 buttonTapped = function(event) local t = event.target local category = t.category if category ~= nil and touchEnabled == true then utils.playSound("select") composer.gotoScene(operations[category].sceneName1, {effect="slideLeft", time=300, params={category=category}}) end return true end buttonTapped1 = function(event) local t = event.target local category = t.category if category ~= nil and touchEnabled == true then utils.playSound("select") composer.gotoScene(operations[category].sceneName2, {effect="slideLeft", time=300, params={category=category}}) end return true end for i=1, #operations do local colour = operations[i].colour local y = (i-0.5)\*(categoryHeight+categoryOffset) + (categoryHeight/1) local textX = 36 local textW = \_W-(textX\*2) - 64 -- an extra 64 for icons local rect = display.newRect(\_W\*0.5, y, \_W, categoryHeight ) rect:setFillColor(colour[6], colour[3], colour[2]) rect.category = i rect:addEventListener("tap", buttonTapped) rect:addEventListener("tap", buttonTapped1) uiGroup:insert(rect) local arrow = display.newSprite(utils.uiSheet, {frames={utils.uiSheetInfo:getFrameIndex("arrow")}}) arrow.x = arrow.width/2 + 4 arrow.y = rect.y uiGroup:insert(arrow) local mainFont = "gomarice\_kaiju\_monster\_0.ttf" local title = display.newText({text=operations[i].title, x=textX, y=rect.y-6, width=textW, height=0, font=mainFont, fontSize=100}) title.anchorX = 0 title:setFillColor(1) uiGroup:insert(title) if operations[i].icon and operations[i].icon ~= "" then local icon = display.newSprite(utils.uiSheet, {frames={utils.uiSheetInfo:getFrameIndex(operations[i].icon)}}) icon.x = \_W - 40 icon.y = rect.y uiGroup:insert(icon) end end end

local M = { { title = "Addition", colour = {245/255, 147/255, 49/255}, sceneName1 = "scenes.difficultyMenuAddition", }, { title = "Subtraction", colour = {240/255, 101/255, 47/255}, sceneName2 = "scenes.difficultyMenuSubtraction", }, } return M

First of all, you’re applying two tap handlers to each button. Even if it actually calls both functions, you can’t go to two different scenes at the same time.

Now your error is happening because you are numbering your scene names (.sceneName1, .sceneName2). But you’re using a “category” value or one or two as an index to a table. As your for loop runs you’re generating:

operations[1].sceneName1

operations[1].sceneName2

operations[2].sceneName1

operations[2].sceneName2

But when category is 1, sceneName2 doesn’t exist and that’s generating the error. If you take the number off of the end of the sceneName and let the category value pick the right thing:

local M = { { title = "Addition", colour = {245/255, 147/255, 49/255}, sceneName = "scenes.difficultyMenuAddition", }, { title = "Subtraction", colour = {240/255, 101/255, 47/255}, sceneName = "scenes.difficultyMenuSubtraction", }, } return M

and

buttonTapped = function(event) local t = event.target local category = t.category if category ~= nil and touchEnabled == true then utils.playSound("select") composer.gotoScene(operations[category].sceneName, {effect="slideLeft", time=300, params={category=category}}) end return true end

Rob

First of all, you’re applying two tap handlers to each button. Even if it actually calls both functions, you can’t go to two different scenes at the same time.

Now your error is happening because you are numbering your scene names (.sceneName1, .sceneName2). But you’re using a “category” value or one or two as an index to a table. As your for loop runs you’re generating:

operations[1].sceneName1

operations[1].sceneName2

operations[2].sceneName1

operations[2].sceneName2

But when category is 1, sceneName2 doesn’t exist and that’s generating the error. If you take the number off of the end of the sceneName and let the category value pick the right thing:

local M = { { title = "Addition", colour = {245/255, 147/255, 49/255}, sceneName = "scenes.difficultyMenuAddition", }, { title = "Subtraction", colour = {240/255, 101/255, 47/255}, sceneName = "scenes.difficultyMenuSubtraction", }, } return M

and

buttonTapped = function(event) local t = event.target local category = t.category if category ~= nil and touchEnabled == true then utils.playSound("select") composer.gotoScene(operations[category].sceneName, {effect="slideLeft", time=300, params={category=category}}) end return true end

Rob