1

我有多关卡游戏,我的所有对象在关卡之间的内存中都保持“空”我的意思是,如果我将物理设置为混合,当我再次进入关卡(或另一个关卡)时,它会显示一个框但没有图片。(和物理作用在这些空盒子上)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 代码进行分组!例如

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 不允许我指定 local group = self.view

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

谢谢您的帮助

4

1 回答 1

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我添加了一个点击侦听器,如果我点击该对象,它将转到第二个场景。destroyScene请注意,当我转到 scene_2 时,我没有费心移除对象和监听器,这是因为当我将对象插入显示组时,每当我更改场景时,它总是会移除对象。因此,在移动到下一个场景后,您可以清除最后一个场景或完全删除它,这样当您回到该场景时,它将重新创建对象。希望这可以帮助您解决问题

于 2013-08-08T16:08:15.000 回答