简体   繁体   中英

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. Why is a nil value returned?

(2) When I go to the next scene because the player dies, how do I destroy the current scene? 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. A scene is an object that has a display.newGroup() as part of the object. It's a member known as "view". That is, "scene.view" is the group.

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. 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. 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. 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:

local sceneGroup = scene.view

and continue using sceneGroup like you are comfortable doing.

Rob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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