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

时间:2016-04-25 03:29:24

标签: lua corona

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

当玩家触摸拾取器时,我希望移除屏幕上所有衍生的射弹(“默认”)(这可能吗?)

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

以下是一些供参考的代码:

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

很抱歉在这里转储所有代码,如果您需要任何确认,请询问。 非常感谢协助

1 个答案:

答案 0 :(得分:1)

每个" 250个单位"你spawnDefault将对象添加到objectGroup。在#34;玩家触摸拾取"的处理程序中,将objectGroup设置为nil。那么你告诉我,spawnDefault下次运行时会在哪里添加对象?