简体   繁体   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>

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

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. 你得到的是错误在线92因为你定义fifteenButton在createScene功能。 as a local object. 作为本地对象。 So it can't be referenced outside the createScene function. 所以它不能在createScene函数之外引用。 To fix that issue you should add local fifteenButton, thirtyButton, sixtyButton to the top of the file. 要解决该问题,您应该将local fifteenButton, thirtyButton, sixtyButton到文件的顶部。 Then remove the local from before the same named variables in the createScene . 然后从createScene相同的命名变量之前删除local This will fix your error. 这将解决您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试调用方法“ addEventListeners”(nil值) - attempt to call method 'addEventListeners' (a nil value) SideMenu 警告:尝试在 swift 中从它的 navigationController == nil 的位置推送一个视图控制器 - SideMenu Warning: attempt to push a View Controller from where its navigationController == nil in swift 更改全局变量崩溃android的值 - Change value of global variable crash android 有没有办法从函数内部更改全局值? - Is there a way to change a global value from inside a function? RangeError(索引):无效值:有效值范围为0 - RangeError (index): Invalid value: Valid value range is 0 Android Studio 按钮错误:尝试在 null object 参考上调用虚拟方法 - Android Studio Button Error: Attempt to invoke virtual method on a null object reference 如何获得下拉选择的索引值而不用单击按钮进行回发 - how to get dropdown selected index value without postback with button click 未捕获的Javascript错误:INDEX_SIZE_ERR:Chrome上的DOM异常1 - Javascript Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1 on Chrome 如何设置全局单击侦听器以从多个按钮中获取值? - How can I set a global click listener to grab the value from many buttons? XButton1 / 2作为全局热键 - XButton1/2 as global hotkey
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM