Corona SDK错误:" ?:0:表索引为nil"当我试图去一个新的场景

时间:2014-03-26 22:52:40

标签: sdk lua storyboard corona

我收到错误说?:0:表索引为零 堆栈追溯: C:在功能'错误' ?:在函数'gotoScene'

错误来自我的代码中的startpressed函数,虽然我认为我的selectlevel.lua文件有问题。

这是我的menu.lua代码:

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local bg
local startb
local aboutb
local title

local function startpressed(event)
if (event.phase == "ended") then
    print("Works")
    storyboard.gotoScene("selectlevel")
end

end

local function aboutpressed(event)
if (event.phase == "ended") then
    print("Works!")
    --storyboard.gotoScene("selectlevel")
end

end



function scene:createScene( event )
local group = self.view
bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight)
startb = display.newImage("startbutton.png")
aboutb = display.newImage("aboutbutton.png")
title = display.newText("Bouncy Shooter", 100, 200,Batang, 75)
title:setFillColor( 0, 0, 0 )
end

function scene:enterScene( event )
local group = self.view
print("entered")
group:insert(bg)
group:insert(startb)

bg.x = display.contentWidth/2
bg.y = display.contentHeight/2

startb.x = display.contentWidth/2
startb.y = display.contentHeight/2 - startb.contentHeight/2 

aboutb.x = display.contentWidth/2
aboutb.y = display.contentHeight/2 + aboutb.contentHeight/1.5

title.x = display.contentWidth/2
title.y = 0 + title.contentHeight

startb:addEventListener("touch", startpressed)
aboutb:addEventListener("touch", aboutpressed)

end




function scene:exitScene(event)
local group = self.view
startb:removeEventListener("touch", startpressed)
aboutb:removeEventListener("touch", aboutpressed)


end


function scene:destroyScene(event)

end



scene:addEventListener( "createScene", scene )


scene:addEventListener( "enterScene", scene )

scene:addEventListener( "exitScene", scene )


scene:addEventListener( "destroyScene", scene )



return scene

这是我的selectlevel.lua代码:

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local box1
local box2
local notdone



function scene:createScene(event)
local group = self.view
box1 = display.newImage("level1button.png")
box2 = display.newImage("level2button.png")

end

function scene:enterScene(event)
local group = self.view
box1.x = display.contentWidth/10 + box1.contentWidth
box1.y = display.contentHeight/10 + box1.contentHeight

box2.x = box1.x + box2.contentWidth * 1.5
box2.y = box1.y 
group:insert(box1)
group:inset(box2)



 end

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


end

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


 end


scene:addEventListener(createScene, scene)

scene:addEventListener(enterScene, scene)

scene:addEventListener(exitScene, scene)

scene:addEventListener(destroyScene, scene)

return scene

你可能会说,我是Corona的新人,所以请不要取笑。

1 个答案:

答案 0 :(得分:2)

您为事件名称使用了变量名而不是字符串。请改用selectlevel.lua

scene:addEventListener('createScene', scene)
scene:addEventListener('enterScene', scene)
scene:addEventListener('exitScene', scene)
scene:addEventListener('destroyScene', scene)