buttonluacoronasdkcorona-storyboard

Error: Attempt to index global 'fifteenButton' (a nil value)


Error Line: 92

Attempt to index global 'fifteenButton' (a nil value)

stack traceback:
[C]: ?
/Users/ejaytumacder/dev/HappyShaker/time_select.lua:92: in function </Users/ejaytumacder/dev/HappyShaker/time_select.lua:90>
?: in function 'dispatchEvent'
?: in function '?'
?: in function 'gotoScene'
/Users/ejaytumacder/dev/HappyShaker/time_select.lua:12: in function '_onRelease'
?: in function '?'
?: in function <?:406>
?: in function <?:218>

This is the error I get above, and below is the code that it originates from.

local storyboard = require("storyboard")
local widget = require("widget")

local scene = storyboard.newScene()

local mydata = require("mydata")

local function fifteenSecondButtonEvent( event )
    local phase = event.phase
    if "ended" == phase then
        mydata.time = 15
        storyboard.gotoScene("play")
    end
end

local function thirtySecondButtonEvent( event )
    local phase = event.phase
    if "ended" == phase then
        mydata.time = 30
        storyboard.gotoScene("play")
    end
end

local function sixtySecondButtonEvent( event )
    local phase = event.phase
    if "ended" == phase then
        mydata.time = 60
        storyboard.gotoScene("play")
    end
end



function scene:createScene( event )
    local group = self.view
    local timeText = display.newText("TIME", 160, 70, "Helvetica", 30)
    group:insert( timeText )

    local fifteenButton = widget.newButton {
        time = 15,
        left = 75,
        top = 150,
        width = 164,
        height = 42,
        defaultFile = "fifteen_button.png",
        overFile = "fifteen_button_pressed.png",
        label = "",
        onRelease = fifteenSecondButtonEvent
    }
    --group:insert(fifteenButton)

    local thirtyButton = widget.newButton {
        time = 30,
        left = 75,
        top = 250,
        width = 164,
        height = 42,
        defaultFile = "thirty_button.png",
        overFile = "thirty_button_pressed.png",
        label = "",
        onRelease = thirtySecondButtonEvent
    }
    --group:insert(thirtyButton)

    local sixtyButton = widget.newButton {
        time = 60,
        left = 75,
        top = 350,
        width = 164,
        height = 42,
        defaultFile = "sixty_button.png",
        overFile = "sixty_button_pressed.png",
        label = "",
        onRelease = sixtySecondButtonEvent
    }
    group:insert(fifteenButton)
    group:insert(thirtyButton)
    group:insert(sixtyButton)
    print( "Number of children in Display Group: " .. group.numChildren )
end

function scene:willEnterScene( event )
    local group = self.view
end

function scene:enterScene( event )
    local group = self.view
end

function scene:exitScene( event )
    local group = self.view
    fifteenButton:removeEventListener( 'onRelease', fifteenSecondButtonEvent ) -- line 92
    thirtyButton:removeEventListener( 'onRelease', thirtySecondButtonEvent )
    sixtyButton:removeEventListener( 'onRelease', sixtySecondButtonEvent )

    timeText:removeSelf()
    timeText = nil
    if fifteenButton then
        fifteenButton:removeSelf()
        fifteenButton = nil
    end

    if thirtyButton then
        thirtyButton:removeSelf()
        thirtyButton = nil
    end

    if sixtyButton then
        sixtyButton:removeSelf()
        sixtyButton = nil
    end

    display.remove(group)
    storyboard.removeScene( "time_select" )

end

function scene:destroyScene( event )
    local group = self.view
end

scene:addEventListener("createScene", scene)
scene:addEventListener("willEnterScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)

return scene

Solution

  • So your question has two problems going on. I'm going to address why you're getting that error.

    You are getting that error on line 92 because you are defining fifteenButton in the createScene function. as a local object. So it can't be referenced outside the createScene function. To fix that issue you should add local fifteenButton, thirtyButton, sixtyButton to the top of the file. Then remove the local from before the same named variables in the createScene. This will fix your error.