使用widget.newButton()函数时出现gotoScene错误。

我正在使用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>

我希望你能在这个问题中找到解决方案。谢谢。 :)

点赞
用户2260604
用户2260604

你应该研究一下 Composer,因为“不仅Composer更易于使用,而且将来也能让我们构建更好、更有趣的东西”

至于你的错误,这很可能是因为storyboard试图调用一个并不存在的函数。我的最佳建议是你阅读Composer的相关文档,并仔细地逐步跟着教程学习。由于不再支持storyboard,而Composer会更容易些。

2014-10-23 12:33:26
用户1870706
用户1870706

我怀疑这是因为您从未向故事板的视图组(即 local group = self.view)添加任何内容。通常,您的背景和按钮应该在故事板的 createScene() 函数中。一旦创建了这些对象,您就可以将它们插入到场景的视图中…

例如,group:insert(background)。因为您的场景视图为空,所以可能会引发该错误。

Rob

2014-10-26 04:40:50