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 は実際に何かを行います。表示グループを削除します。宣言が1つしかなく、そのようなコードがないため、あまり明白ではありません

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

関数 createPuppet では local group = self.view を指定できないため、(group) を渡しました。

私のメールアドレスはedualczatebed@gmail.comです

ご協力ありがとうございました

4

1 に答える 1

0

あなたのコードには多くのバグがあり、それを実装する方法は良くありません。シーンに移動してから別のシーンに切り替えると、上のすべてのオブジェクトをdestroyScene一緒に削除するため、非常に効率的ではないリスナーです。ポイントすることはできません私が言ったように、コードには多くのバグがあるため、問題の原因を突き止めてください。コロナで絵コンテの使い方がよくわからなかったと思います。問題をどのように解決するかについてのアイデアを提供するために、オブジェクトを削除しなくても、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タップすると 2 番目のシーンに移動するタップ リスナーを追加しました。destroySceneシーンを変更するたびにオブジェクトを表示グループに挿入すると、常にオブジェクトが削除されるため、scene_2 に移動するときにオブジェクトとリスナーを削除することを気にしなかったことに注意してください。そのため、次のシーンに移動した後、最後のシーンをパージするか、完全に削除して、そのシーンに戻ったときにオブジェクトを再作成することができます。これが問題の解決に役立つことを願っています

于 2013-08-08T16:08:15.000 に答える