尽管removeSelf +设置为nil,但不会在级别之间清除Corona +对象

时间:2013-08-07 12:24:46

标签: object corona

我有多级游戏,所有对象在关卡之间保持“空” 我的意思是,如果我将物理设置为混合,当我再次进入关卡时,(或其他关卡)它会显示一个框但没有图片。 (物理作用于这些空盒子) 在destroyScene我已经确定他们都是

   myObj:removeSelf()
    myObj = nil

我打印一条消息,证明它确实是这样做的。

同样在enterScene的菜单中,以防万一我做

  local prior_scene = storyboard.getprevious()
  storyboard.purgescene( prior_scene )

并尝试了

  storyboard.removeAll()

甚至是

  storyboard.purgeOnSceneChange = true

当我进入下一个级别或再次进入同一级别时,没有任何作用,我之前的所有对象仍然在这里,我只是没有得到它

好吧,它会有点长,但在整个关卡。它确实通过了destroyscene但不知何故显示对象没有被移除。

-- scene5
----------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene5 = storyboard.newScene()

function scene5:createScene( event )
    local group = self.view
  puppetJColCount = 0
  print ("createScene5", puppetJColCount)
    -----------------------------------------------------------------------------
    --  CREATE display objects and add them to 'group' here.
    --  Example use-case: Restore 'group' from previously saved state.
    local physics = require("physics")
    physics.start()
    --physics.setScale(50)
    puppetT_outside = false
    puppetJ_outside = false

end -- scene -------------------------


-- set and release the catapult for puppetT
function arm_puppetT(event)
    if event.phase == "began" then
        display.getCurrentStage():setFocus(puppetT)
        arming_puppetT = true
    elseif event.phase == "moved" then
        puppetT.x = event.x
        puppetT.y = event.y
    elseif event.phase == "ended" then
            puppetT:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, puppetT.x, puppetT.y)
            display.getCurrentStage():setFocus(nil)
            arming_puppetT = false
    end
end -- arm_puppetT ----------------------------------------------------------------------------

function build_puppetT()
-- setup puppetT        
    puppetT = display.newImage("assets/t-head-57-78.png")
    puppetT.x = 80
    puppetT.y = 100
    physics.addBody(puppetT,"dynamic",{density=1.0, friction =0.3, bounce=0.2})
-- setup catapult event for arming puppetT
    puppetT:addEventListener("touch", arm_puppetT) 
end -- build_puppetT

function build_puppetJ_wall()
 puppetJColCount = puppetJColCount + 1  -- how many columns of puppetJ
    puppetJIJ = {}              -- define puppetJ as an array
    ij = 0
    ipuppetJtot = 0
--puppetJColCount = 1
    print ("build_puppetJ_wall puppetJColCount>" , puppetJColCount);
    for i=1, 4 do
        for j=1, puppetJColCount do
        ij = ij + 1         -- # of puppetJs on the screen
        ipuppetJtot = ipuppetJtot + 1
        puppetJIJ = display.newImageRect("assets/j-head-70-75.png",80,75)

        puppetJIJ.x = 600 + j*22
        puppetJIJ.y = 100 + (puppetJIJ.height /2 * (i -1))

        physics.addBody(puppetJIJ,"dynamic",{density=1.0,friction=0.3,bounce=0.2,isSensor=false,radius = var})
        end
    end
print ("building puppetJs #:" ,ipuppetJtot)
end -- build_puppetJ_wall -------------------------------------------------------------


function every_frame( event )
end -- every_frame --------------------------------------------------------------------

--reset level
function tap_reset_level(event)
        puppetT:applyLinearImpulse(0, 0, 0, 0)  -- stop the kick
        print "restarting physics?"
        puppetT:setLinearVelocity( 0, 0 )   -- stop the speed
    puppetT.x = 80
    puppetT.y = 100

    for ij = 1,ipuppetJtot do
        if (puppetJIJ) then
            puppetJIJ:removeSelf()
            puppetJIJ = nil ------
        end
    end
  puppetT:removeSelf()
  puppetT = nil

    build_puppetJ_wall()
  build_puppetT()

    puppetT_outside = false
    puppetJ_outside = false
    -- physics.addBody(puppetT,"dynamic",{density=1.0, friction =0.3, bounce=0.2})
    -- puppetT:addEventListener("touch", arm_puppetT)
    --physics.start()

end -- tap_reset_level -------------------------------------------------------------------------

function tap_main_menu(event)
print ("going to main menu")
  Runtime:removeEventListener("enterFrame", every_frame)  
    for ij = 1,ipuppetJtot do
        if (puppetJIJ) then
            puppetJIJ:removeSelf()
            puppetJIJ = nil ------
        end
    end
    if (puppetT) then
        puppetT:removeSelf()
        puppetT = nil
    end


    -- scene5:exitScene(scene5)
    storyboard.gotoScene( "menu" )  
end --  tap_main_menu ---------------------------------------------------------------------------------
-- ======================================================================================
-- Called immediately after scene has moved onscreen:
function scene5:enterScene( event )
    local group = self.view

    -----------------------------------------------------------------------------
    --  INSERT code here (e.g. start timers, load audio, start listeners, etc.)
--  group:insert( reset_btn )
-- load background
print ("enterScene scene5 " , puppetJtot)

    background = display.newImage("assets/parliament-1200-800.png",0,0)
--create reset level button
    reset_btn = display.newImageRect( "assets/btn_reset_128.png", 50,50)
    reset_btn.x = 50
    reset_btn.y = 50

--create main menu button
    main_menu_btn = display.newImageRect( "assets/btn_home_128.png", 50,50)
    main_menu_btn.x = 100
    main_menu_btn.y = 50

-- show the level
    local myText = display.newText( "Level 5", display.contentWidth - 60, 50,  "Helvetica", 24 )
    myText:setTextColor(255, 255, 255)

--  insert(floor);

    floor = display.newRect(20,display.contentHeight - 40,display.contentWidth - 40 ,10)
    physics.addBody(floor,"static",{density=1.0, friction =-0.3,bounce=-0.2,isSensor=false})

-- build puppetT
  build_puppetT()


print ("width=" , display.contentWidth , "height=",display.contentHeight)


-- setup puppetJs
    build_puppetJ_wall()

-- everything happens in everyframe function
Runtime:addEventListener("enterFrame", every_frame)

--add reset event
    reset_btn:addEventListener( "tap", tap_reset_level ) 
--add mainmenu event
    main_menu_btn:addEventListener( "tap", tap_main_menu )  
end -- scene:enterScene ----------------------------------------------------------------
-- ======================================================================================


-- Called when scene is about to move offscreen:
function scene5:exitScene( event )
    local group = self.view
print "scene:exitScene5"
    -----------------------------------------------------------------------------
    --  INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
end -- exitScene


-- Called prior to the removal of scene's "view" (display group)
function scene5:destroyScene( event )
    local group = self.view

    Runtime:removeEventListener("enterFrame", every_frame)
    puppetJColCount = 0

    if (reset_btn) then
print ("destroyScene5 - removing tap_reset_level")
        reset_btn:removeEventListener( "tap", tap_reset_level ) 
        reset_btn:removeSelf()
    end
        reset_btn = nil
    if (main_menu_btn) then
        main_menu_btn:removeEventListener( "tap", tap_main_menu )
        main_menu_btn:removeSelf()
    end
        main_menu_btn = nil
    for ij = 1,ipuppetJtot do
        if (puppetJIJ) then
            puppetJIJ:removeSelf()
        end
            puppetJIJ = nil ------
    end

    if (puppetT) then
    puppetT:removeSelf()

  end
    puppetT = nil

 scene5 = nil

end -- destroyScene ------------------------------------------------------------



scene5:addEventListener( "createScene", scene5 )

scene5:addEventListener( "enterScene", scene5 )

scene5:addEventListener( "exitScene", scene5 )

scene5:addEventListener( "destroyScene", scene5 )

---------------------------------------------------------------------------------

return scene5

更新

你好@DevfaR

哈,我已经从头开始用电晕模板重新启动了一些东西:

1)如果使用本地组对显示对象进行分组,则exitScene / destroyScene实际上会执行某些操作。它删除了显示组! 不是很明显,因为其中只有一个声明,没有这样的代码

2)为什么我在这个地方有如此多的removeSelf对象的原因是因为没有任何工作。所以我在网络上尝试了很多东西。

3)它之所以不起作用,是因为我将显示对象创建为一个函数。并以某种方式显示组不通过那里。如果我将代码移动到createScene函数中,那么在进入下一个场景时确实会清除它。

问题是..我真的想把我的puppetCreation代码分组! e.g。

function scene:createScene( event )
        local group = self.view
        local physics = require("physics")
        physics.start()
        background = display.newImage("assets/bckg.png",0,0)
    group:insert(background)

       createPuppet1(group)
       createPuppet2(group)
end
function createPuppet1(group)
        puppet1= display.newImage("assets/puppet1.png",0,0)
    group:insert(puppet1)
end
function createPuppet2(group)
        puppet2= display.newImage("assets/puppet2.png",0,0)
    group:insert(puppet2)
end

我传递了(group)因为createPuppet函数不允许我指定 本地组= self.view

我的电子邮件地址是edualczatebed@gmail.com

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

你的代码中有很多错误,你实现它的方式并不好,因为当你去一个场景,然后切换到另一个场景时,你将destroyScene上的所有对象与它的监听器一起移除这是不是非常有效,我不能指出是什么导致了问题,因为就像我说你的代码中有很多错误。我觉得你不太明白如何在Corona上使用故事板。为了让您了解如何解决问题,我将为您实现一些代码,以便了解即使您不删除destroyScene上的对象,也可以通过显示组上的对象删除对象。

local rectangle
local circle

local function nextScene(event)
  storyboard.gotoScene( "scene_2", "fade", "500")

end

-- Called when the scene's view does not exist:
function scene:createScene( event )
    local group = self.view

    -----------------------------------------------------------------------------

    rectangle = display.newRect(100,100,100,100)
    rectangle:setFillColor(255,0,0)
    rectangle.x = 100
    rectangle.y = 100

    circle = display.newCircle(100,100,50)
    circle.x = 250
    circle.y = 100
    circle:setFillColor(0,255,0)

    --inserting all of your object into display group so that if you destroy the scene the display group will be remove
    group:insert(rectangle) 
    group:insert(circle)


    -----------------------------------------------------------------------------

end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    local group = self.view

    -----------------------------------------------------------------------------

    rectangle:addEventListener("tap", nextScene)
    circle:addEventListener("tap", nextScene)



    -----------------------------------------------------------------------------

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
    local group = self.view

    -----------------------------------------------------------------------------


    -----------------------------------------------------------------------------

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
    local group = self.view

    -----------------------------------------------------------------------------



    -----------------------------------------------------------------------------

end

你可以看到我创建了2个对象矩形和圆形我创建了对象后将其插入到组然后在enterScene我添加了一个点击监听器,如果我点击该对象,它将转到第二个场景。请注意,当我转到scene_2时,我没有费心去除destroyScene上的对象和监听器,因为当我更改场景时将对象插入显示组时,它将始终删除对象。因此,在移动到下一个场景后,您可以清除最后一个场景或完全删除它,这样当您返回到该场景时,它将重新创建该对象。 希望这可以帮助您解决问题