2017-04-01 48 views
0

我希望這個場景中的氣球(即DisplayObject)在場景結束時消失。我已將它們作爲第一個參數傳遞給local sceneGroup = scene.view,並將其作爲第一個參數傳遞給display.newImageRect(),並認爲這將足以讓它們在底部的destroy.scene位中列出。然而,當它轉換到下一個場景時,他們仍然在那裏。爲什麼這些DisplayObject不會被刪除?如何在場景結束時刪除這些DisplayObject?

local composer = require("composer") 

local scene = composer.newScene() 


local Balloons 

local positioninsheetOptions = 100 


local sheetOptions = 
{ 
    frames = 
    { 
     { 
      x = 0, 
      y = 0, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*2, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*3, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*4, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*5, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*6, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*7, 
      width = 71, 
      height = 100 
     }, 
     { 
      x = 0, 
      y = positioninsheetOptions*8, 
      width = 71, 
      height = 100 
     }, 

     { 
      x = 0, 
      y = positioninsheetOptions*9, 
      width = 71, 
      height = 100 
     }, 

      { 
      x = 0, 
      y = positioninsheetOptions*10, 
      width = 71, 
      height = 100 
     }, 


      { 
      x = 0, 
      y = positioninsheetOptions*11, 
      width = 71, 
      height = 100 
     }, 

     }, 
     } 
local objectSheet = graphics.newImageSheet("gameObjects.png", sheetOptions) 
Balloons = {} 

-- ----------------------------------------------------------------------------------- 
-- Code outside of the scene event functions below will only be executed ONCE unless 
-- the scene is removed entirely (not recycled) via "composer.removeScene()" 
-- ----------------------------------------------------------------------------------- 

local function gotoGame() 
    composer.gotoScene("game") 
end 




-- ----------------------------------------------------------------------------------- 
-- Scene event functions 
-- ----------------------------------------------------------------------------------- 

-- create() 
function scene:create(event) 

    local sceneGroup = self.view 
    -- Code here runs when the scene is first created but has not yet appeared on screen 

    local background = display.newImageRect(sceneGroup, "background.png", 800, 1400) 
    background.x = display.contentCenterX 
    background.y = display.contentCenterY 





     local balloonBlue1 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100) 
     local balloonYellow1 = display.newImageRect(sceneGroup, objectSheet, 8, 71, 100) 
     local balloonRed1 = display.newImageRect(sceneGroup, objectSheet, 10, 71, 100) 
     local balloonBlue2 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100) 

     local Balloons = display.newGroup() 

    Balloons:insert(balloonBlue1) 
    Balloons:insert(balloonYellow1) 
    Balloons:insert(balloonRed1) 
    Balloons:insert(balloonBlue2) 


    Balloons:addEventListener("tap", gotoGame) 

    balloonBlue1.x = (display.contentWidth/8) 
    balloonBlue1.y = balloonBlue1.height 

     balloonYellow1.x = (display.contentWidth/8)*3 
    balloonYellow1.y = balloonBlue1.height 

     balloonRed1.x = (display.contentWidth/8)*5 
    balloonRed1.y = balloonBlue1.height 

    balloonBlue2.x = (display.contentWidth/8)*7 
    balloonBlue2.y = balloonBlue1.height 

end 


-- show() 
function scene:show(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 
     -- Code here runs when the scene is still off screen (but is about to come on screen) 

    elseif (phase == "did") then 
     -- Code here runs when the scene is entirely on screen 

    end 
end 


-- hide() 
function scene:hide(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 

    elseif (phase == "did") then 
     -- Code here runs immediately after the scene goes entirely off screen 

    end 
end 


-- destroy() 
function scene:destroy(event) 

    local sceneGroup = self.view 

    -- Code here runs prior to the removal of scene's view 

end 


-- ----------------------------------------------------------------------------------- 
-- Scene event function listeners 
-- ----------------------------------------------------------------------------------- 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 
-- ----------------------------------------------------------------------------------- 

return scene 

回答

2

您需要的Balloon組(其中的氣球是兒童)添加到您的sceneGroup

scene:destroy,請確保您刪除任何偵聽器並取消任何想要銷燬的東西的轉換。如果你有,當場景的視圖被破壞時,Balloon組及其所有的孩子也將被銷燬。

+0

完美。再次感謝。我認爲「display.newImageRect(sceneGroup,objectSheet,6,71,100)」將添加到sceneGroup,但也許一個對象不能屬於兩個組? – Atrag

+1

該對象只能屬於一個組。請參閱[文檔](https://docs.coronalabs.com/api/type/GroupObject/insert.html) - 陷阱。 – ldurniat