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