将生成的对象插入到组中并删除场景

时间:2015-07-18 01:08:35

标签: lua null corona corona-storyboard

根据下面的代码块,我有两个主要问题:

(1)在两个产生函数中,即使我已经命名了sceneGroup并将生成的对象插入到一个新组中,sceneGroup:insert()方法也会返回nil。为什么返回零值?

(2)当我因为玩家死亡而进入下一个场景时,如何销毁当前场景?我试过获取当前场景并将其删除,但它没有奏效。

local function spawnObjects()
    local bananas = display.newGroup()
    sceneGroup:insert(bananas)
    bananas = display.newImageRect("object_bananas.png", 30, 20);
    physics.addBody(bananas, "dynamic", {bounce = 0.3, radius = 9.5});
    bananas.x = math.random(0, 320);
    bananas.y = -40;
    transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310)+1 , y = 600, });

    -- function to handle the collision on the bananas
    function bananas:collision(e)
        -- only perform logic when the bananas are colliding with monkey
        if (e.other.class == "monkey") then
    updateScore()
            -- cannot remove objects during a collision, so wait a short moment for it to end
            timer.performWithDelay(100, function()
                -- remove the bananas
                display.remove(self)
            end, 1)
        end
        -- always return true because we have handled the collision
        return true
    end
    -- attach a collision listener to the bananas
    bananas:addEventListener("collision",bananas)
    return bananas

end
local total_bananas = 15
tmr = timer.performWithDelay(2000, spawnObjects, total_bananas);


local function spawnObjects()
    local coconut = display.newGroup()
    sceneGroup:insert(coconut)
    coconut = display.newImageRect("coconut.png", 30, 35)
    physics.addBody(coconut, "dynamic", {bounce = 0.5, radius = 11.5});
    coconut.x = math.random(0, 320);
    coconut.y = -40;
    transition.to( coconut, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, });

    -- function to handle the collision on the bananas
    function coconut:collision(e)
        -- only perform logic when the coconut is colliding with monkey
        if (e.other.class == "monkey") then
            -- cannot remove objects during a collision, so wait a short moment for it to end
            timer.performWithDelay(1, function()
                -- remove the coconut
                display.remove(self)
            end, 1)
    composer.gotoScene("scene_gameOver")
    end

        -- always return true because we have handled the collision
        return true
    end

    -- attach a collision listener to the bananas
    coconut:addEventListener("collision", coconut)
    return coconut
end
local total_coconut = 15
tmr = timer.performWithDelay(2000, spawnObjects, total_coconut);

1 个答案:

答案 0 :(得分:0)

暂时忘掉sceneGroup。场景是一个对象,它具有display.newGroup()作为对象的一部分。它是一个名为" view"的成员。也就是说," scene.view"是小组。

因为你可以使用Composer做一些高级的事情,比如在一个组中有多个场景(不是我推荐它),场景的事件函数如scene:create()等以面向对象的方式处理。通过在场景和创建之间使用冒号运算符(:),您将传递一个名为" self"的隐式参数。这是触发事件的场景对象。在大多数情况下,自我和场景都是一样的。

为了方便开发人员,我们在事件函数中对scene.view(self.view)进行本地引用,以便于您使用。如果您需要访问其中一个事件函数之外的场景视图组,只需执行以下操作:

scene.view:insert( displayObjectOfYourChoice )

或者你可以本地化你自己的sceneGroup变量:

local sceneGroup = scene.view

并继续使用sceneGroup,就像你很自在。

罗布