我正在使用Corona SDK制作游戏应用,当我点击我的界面中尝试加载第一个场景的开始按钮时,我收到了错误。
以下是我的代码:
main.lua
`local storyboard = require "storyboard"
storyboard.gotoScene("menu")`
menu.lua
`local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require( "widget" )`
`local background = display.newImage("Legend of Kael.jpg", true)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2`
`local roundedRect = display.newRoundedRect( 10, 50, 300, 40, 8 )
roundedRect.anchorX, roundedRect.anchorY = 0.0, 0.0
roundedRect:setFillColor( 0/255, 0/255, 0/255, 170/255 )`
`local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
storyboard.gotoScene( "scene1", "crossFade", 400)
end
end`
`local button = widget.newButton
{
defaultFile = "buttonBlue.png",
overFile = "buttonBlueOver.png",
label = "Start Game",
emboss = true,
onEvent = handleButtonEvent,
}`
`button.x = 240; button.y = 200`
`function scene:createScene( event )
local group = self.view
end`
`function scene:enterScene( event )
storyboard.purgeScene("main")
local group = self.view
end`
`function scene:exitScene( event )
local group = self.view
storyboard.removeScene("main")
end`
`scene:addEventListener("createScene", scene)
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )`
`return scene`
scene1.lua
`local physics = require "physics"
physics.start()`
`local storyboard = require ("storyboard")
local scene = storyboard.newScene()`
`function scene:createScene(event)`
`local background = display.newImage("forest1.jpg" ,display.contentWidth, display.contentHeight)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2`
`local ground = display.newImage("ground.png")
ground:setReferencePoint(display.BottomLeftReferencePoint)
ground.x, ground.y = 0,420`
`local groundShape = {-420, -20, 420, -20, 420, 20, -420, 20}
physics.addBody(ground, "static", {friction = 1.0, density = 1.0, bounce = 0, shape = groundShape})`
`local char = display.newImage("char.png")
char.x = 70
char.y = 230`
`physics.addBody(char, {friction = 1.0, density = 1.0, bounce = 0.3, radius = 35})
char.isFixedRotation = true`
`local function onScreenTouch(event)
if event.phase == "began" then
char:applyForce(100, -500, char.x, char.y)
end`
`return true
end`
`end`
`Runtime:addEventListener("touch", onScreenTouch)`
`scene:addEventListener("createScene", scene)`
`return scene`
我得到的错误是:
`assertion failed!
stack traceback:`
`[C]: in function 'error'
?: in function 'gotoScene'
menu.lua:17:in function '_onEvent'
?: in function '?'
?: in function <?:424>
?: in function <?:218>`
我希望你能找到解决这个问题的方法。谢谢.. :))
答案 0 :(得分:0)
你应该研究Composer因为 “不仅Composer更容易使用,而且它还可以让我们在将来构建更好,更有趣的东西。” 强>
关于您的错误,这很可能是因为故事板试图调用实际上不可用的函数。我最好的建议是你阅读有关作曲家的内容并仔细阅读教程。由于故事板不再受支持,因此作曲家更容易。
答案 1 :(得分:0)
我怀疑它是因为你永远不会在故事板的视图组中添加任何东西(即本地组= self.view)。通常,您的背景和按钮将位于故事板的createScene()函数中。创建这些对象后,您可以将它们中的每一个插入到场景的视图中......
group:insert( background )
例如。由于您的场景视图为空,因此可能会抛出该错误。
罗布