如何从屏幕上删除所有生成的对象?

How can I remove all spawned objects from the screen?

我正在尝试制作一个简单的躲避游戏,其中包括从屏幕顶部掉落的拾取物:"wpLaser"

当玩家触摸拾取器时,我希望屏幕上所有生成的投射物 ("default") 都被移除(这可能吗?)

问题是我希望射弹在拾取物被拾起后继续产卵

部分代码供参考:

local composer = require("composer")
local widget = require("widget")
local scene = composer.newScene()

local physics = require( "physics" ) -- Using physics for collision detections
physics.start()
physics.setGravity( 0, 0 )

-- Object group for removal
local objectGroup = display.newGroup()

-- Set Variables
_W = display.contentWidth; -- Get the width of the screen
_H = display.contentHeight; -- Get the height of the screen

function scene:create( event )
    local sceneGroup = self.view

以下是我想要在玩家触摸拾取器时移除的射弹:

-- Projectiles
    local numberDefault = 1 --local variable; amount can be changed

    local function clearDefault( thisDefault )
        display.remove( thisDefault ) ; thisDefault = nil
    end

    local function spawnDefault()
        for i=1,numberDefault do
            local default = display.newImage( "projectiles/default.png" )
            default.x = math.random( 0, _W )
            default.y = -100
            default.myName = "default"
            default.class = "default"

            physics.addBody( default, "dynamic", { density = 0, friction = 0, bounce = 0, isSensor = true, radius = 30 } )
            transition.to( default, { x = math.random( 0, _W ), y = 1200, time = 4000, onComplete = clearDefault } )

            objectGroup:insert( default )
        end
    end

    timerDefault = timer.performWithDelay( 250, spawnDefault, 0 )  -- spawn 1 every 250 units

这是皮卡本身:

-- Laser power-up
    local numberWpLaser = 1 --local variable; amount can be changed

    local function clearWpLaser( thisWpLaser )
        display.remove( thisWpLaser ) ; thisWpLaser = nil
    end

    local function spawnWpLaser()
        for i=1,numberWpLaser do
            local wpLaser = display.newImage( "images/wpLaser.png" )
            wpLaser.x = math.random( 0, _W )
            wpLaser.y = -100
            wpLaser.myName = "wpLaser"

            physics.addBody( wpLaser, "dynamic", { density = 0, friction = 0, bounce = 0, isSensor = true, radius = 40 } )
            transition.to( wpLaser, { x = wpLaser.x, y = 1200, time = 5000, onComplete = clearWpLaser } )

            objectGroup:insert( wpLaser )

        end
    end

    timerWpLaser = timer.performWithDelay( 5000, spawnWpLaser, 0 )  -- spawn 1 every 5000 units

-- Collision detection events
    local function onCollision( event )
        if event.phase == "began" then -- event is only called when it begins (not when it ends)
            if ( event.object1.myName == "player" and event.object2.myName == "default") then
                print( "You Died" )

这是我遇到困难的地方。当玩家触摸拾取器时,我希望屏幕上所有生成的对象都被移除,但继续生成。 目前,抛射物正在被移除,但我无法让它们再次开始产卵。

            elseif ( event.object1.myName == "player" and event.object2.myName == "wpLaser") then
                event.object2:removeSelf()
                transition.cancel( default )

                timer.pause( timerDefault )
                timer.pause( timerWpLaser )

                local pickupSound = audio.loadSound( "audio/pickup.mp3" )
                local pickupChannel = audio.play( pickupSound )

                local blank = display.newRect( _W/2, _H/2, _W, _H )
                blank.myName = "blank"
                transition.to( blank, { time = 800, alpha = 0, onComplete = clearblank } )

                objectGroup:removeSelf( )
                objectGroup = nil

                print( "Pickup" )
            end
        end
    end

Runtime:addEventListener( "collision", onCollision )
end

抱歉将所有代码转储到此处,如果您需要任何确认,请询问。 非常感谢您的帮助

每“250 个单位”spawnDefault 将对象添加到 objectGroup。在 "player touches the pickup" 的处理程序中,将 objectGroup 设置为 nil。那么你告诉我,spawnDefault 将在下次运行时将对象添加到哪里?