簡體   English   中英

錯誤:嘗試索引全局'fifteenButton'(零值)

[英]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>

這是我上面得到的錯誤,下面是它源自的代碼。

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

所以你的問題有兩個問題。 我將解決你為什么會收到這個錯誤。

你得到的是錯誤在線92因為你定義fifteenButton在createScene功能。 作為本地對象。 所以它不能在createScene函數之外引用。 要解決該問題,您應該將local fifteenButton, thirtyButton, sixtyButton到文件的頂部。 然后從createScene相同的命名變量之前刪除local 這將解決您的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM