为什么group:在故事板场景改变后插入失败?

时间:2014-01-28 19:03:41

标签: lua corona

下面的代码示例来自单个故事板场景。放大镜(mg),绿色圆圈(asset1),返回按钮和动画船(船)都会在展开时添加到场景的显示组中。在此期间,还将使用display.remove(ship)再次移除该船。 这在第一次创建场景时工作正常,但是,当我单击返回按钮,进行场景更改,然后返回到此场景时,代码在尝试将船舶添加到显示组时失败:“尝试调用方法'insert'(零值)“

是什么给出了?

    local storyboard = require ("storyboard")
    local widget = require ("widget")
    local scene = storyboard.newScene()
    storyboard.purgeOnSceneChange = true

    function scene:createScene(event)
        local group=self.view
        local asset1x = 600
        local asset1y = 400

    -- MAGNIFYING GLASS
        local mg=display.newImage( "images/mg.jpg" )
        mg.x=600
        mg.y=150
        mg.myName = "mg"
        physics.addBody(mg,"dynamic", { density = 1.0, friction = 0.3, bounce = 0.2, radius = mgRadius})    
        group:insert(mg)            

    -- ASSET PLANT
        local asset1 = display.newCircle(asset1x,asset1y,30)
        asset1:setFillColor(100,200,300)
        physics.addBody(asset1,"static")
        asset1.myName = "asset1"
        asset1.isSensor = true
        asset1.isVisible = false
        group:insert(mg)

        asset1.collision = function(self,event)
            if (event.phase == "began" and event.other.myName == "mg") then
                print("began")
                ship=display.newSprite( shipSheet, shipSeqData )
                ship:play()
                ship:scale(2,2)
                ship.x=asset1x
                ship.y=asset1y
                ship.myName = "ship"
                group:insert(ship)
            elseif (event.phase == "ended" and event.other.myName == "mg") then
                print("ended")
                display.remove(ship)
            end
        end
        asset1:addEventListener("collision")

    -- RETURN BUTTON
        local function returnWelcome(event)
            storyboard.gotoScene("000","fade", 600)
        end
        local returnButton = widget.newButton
        {
            left=900,
            top=385,
            width=300,
            height=225,
            defaultFile="images/bg000.jpg",
            onRelease=returnWelcome,
        }
        group:insert(returnButton)

    end

    scene:addEventListener( "createScene", scene )

    return scene

1 个答案:

答案 0 :(得分:1)

事实证明我不小心将mg显示对象添加到显示组两次并且从未添加过asset1 - 这在某种程度上导致了错误。