【问题标题】:Inserting spawned objects into a group and removing the scene将生成的对象插入组并移除场景
【发布时间】:2015-10-07 19:44:02
【问题描述】:

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

(1) 在这两个生成函数中,sceneGroup:insert() 方法返回 nil,即使我已经命名了 sceneGroup 并将生成的对象插入到一个新组中。为什么返回 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);

【问题讨论】:

    标签: lua null coronasdk corona-storyboard


    【解决方案1】:

    暂时忘记场景组。场景是具有 display.newGroup() 作为对象一部分的对象。它是一个被称为“视图”的成员。即“scene.view”为组。

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

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

    scene.view:insert( displayObjectOfYourChoice )
    

    或者您可以本地化您自己的场景组变量:

    local sceneGroup = scene.view
    

    并继续以您喜欢的方式使用 sceneGroup。

    罗伯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多