简体   繁体   English

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

[英]Inserting spawned objects into a group and removing the scene

I have two main questions based on the code block below: 基于下面的代码块,我有两个主要问题:

(1) In both spawning functions, the sceneGroup:insert() method returns nil even though I have already named sceneGroup and have inserted spawned objects into a new group. (1)在这两个生成函数中,即使我已经命名了sceneGroup并将生成的对象插入到新的组中,sceneGroup:insert()方法仍返回nil。 Why is a nil value returned? 为什么返回nil值?

(2) When I go to the next scene because the player dies, how do I destroy the current scene? (2)由于玩家死亡而进入下一个场景时,如何销毁当前场景? I've tried obtaining current scene and removing it that way, but it hasn't worked. 我尝试获取当前场景并以这种方式将其删除,但是它没有用。

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);

Forget about sceneGroup for a moment. 暂时忘掉SceneGroup。 A scene is an object that has a display.newGroup() as part of the object. 场景是一个对象,其中具有display.newGroup()作为对象的一部分。 It's a member known as "view". 它是被称为“视图”的成员。 That is, "scene.view" is the group. 也就是说,“ scene.view”是该组。

Because you can do some advanced things with Composer like have multiple scenes in one group (not that I would recommend it) the scene's event functions like scene:create() and such are handled in an object oriented manner. 因为您可以使用Composer做一些高级的事情,例如在一个组中有多个场景(并非我会推荐这样做),所以场景的事件函数(例如scene:create())以面向对象的方式处理。 By using the colon operator (:) between scene and create, you are passing an implicit parameter called "self" that is the scene object that triggered the event. 通过在场景和创建之间使用冒号运算符(:),您将传递一个称为“ self”的隐式参数,该参数是触发事件的场景对象。 In most cases self and scene are the same thing. 在大多数情况下,自我和场景是同一回事。

As a convenience to developers we make a local reference to scene.view (self.view) inside the event functions to make it easy for you to use. 为了方便开发人员,我们在事件函数中局部引用了scene.view(self.view),以使其易于使用。 If you need to access the scene's view group outside of one of these event functions simply do: 如果您需要在这些事件功能之一之外访问场景的视图组,只需执行以下操作:

scene.view:insert( displayObjectOfYourChoice )

or you could localize your own sceneGroup variable: 或者您可以本地化自己的sceneGroup变量:

local sceneGroup = scene.view

and continue using sceneGroup like you are comfortable doing. 并像您喜欢的那样继续使用sceneGroup。

Rob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM