繁体   English   中英

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

[英]How can I remove all spawned objects from the screen?

我正在尝试制作一个简单的躲避游戏,其中包括一个从屏幕顶部掉落的皮卡:“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

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

每一个“250个部队”你spawnDefault这增加了对象对象组。 在“玩家触摸拾音器”的处理程序中,您将 objectGroup 设置为 nil。 那么你告诉我, spawnDefault下次运行时会在哪里添加对象?

暂无
暂无

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

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