2015-12-27 62 views
0

我正在使用Corona SDK在lua上開發應用程序。composer.removeScene()不工作,其他顯示對象不會消失

我想從我的buildscene2到我的mainbuildscene,我成功地調用了新的場景,但buildscene2中的對象沒有被刪除。我在mainbuildscene.lua文件中添加了刪除前一場景的代碼。我試過在場景中添加它:create()和scene:show()。都沒有工作。從同一個應用程序中的其他場景轉換時,這種確切的方法適用於我。我在這裏所做的是創建一個函數,它使用composer.gotoScene(「mainbuildscene」),然後在按下按鈕時調用該函數。我有一堆隨機打印只是爲了看代碼是否被讀取。它是。

我找過其他人有這個問題。很多人都經歷過這種情況,但他們的解決方案都不適合我。我試着移動事件監聽器,以便將下一個按鈕按下到snapTo函數內部,然後退出。如果聽衆在外面,它會給我一個錯誤。

另外,我知道這個問題並不是在我打電話給新場景的時候,因爲我隨機放置了一張圖片來測試這個場景中的物體是否會出現,並且他們確實會這樣做,除非它們超出了來自buildscene2的對象。我甚至嘗試在第二個場景中添加一個函數,當被調用時,它將銷燬第一個場景。沒有工作。

除非我做錯了什麼,否則我在buildscene2中的所有顯示對象都會添加到組中。

這是我的buildscene2文件。這是我正在轉移並試圖銷燬的文件。

local composer = require("composer") 

local scene = composer.newScene() 

-- ----------------------------------------------------------------------------------------------------------------- 
-- All code outside of the listener functions will only be executed  ONCE unless "composer.removeScene()" is called. 
-- ----------------------------------------------------------------------------------------------------------------- 

-- local forward references should go here 

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


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

local sceneGroup = self.view 

-- Initialize the scene here. 
-- Example: add display objects to "sceneGroup", add touch listeners, etc. 

local group = display.newGroup() 
composer.removeScene("thirdscene") 



----------EMPTY BOXES 
local itemFrame1=display.newImageRect("images/selection-box.png", 200,200) 
itemFrame1.x=140 
itemFrame1.y=130 
group:insert(itemFrame1) 

local itemFrame2=display.newImageRect("images/selection-box.png", 200,200) 
itemFrame2.x=387 
itemFrame2.y=130 
group:insert(itemFrame2) 

local itemFrame3=display.newImageRect("images/selection-box.png", 200,200) 
itemFrame3.x=635 
itemFrame3.y=130 
group:insert(itemFrame3) 

---------------HEADS 
local robotHead=display.newImageRect("images/robot-head.png", 195,180) 
robotHead.x=137 
robotHead.y=130 
robotHead.rotation = 180 
robotHead.headtype=1 

local rabbitHead=display.newImageRect("images/rabbit-head.png", 200, 180) 
rabbitHead.x=387 
rabbitHead.y=130 
rabbitHead.rotation = 180 
rabbitHead.headtype=2 

local dinoHead=display.newImageRect("images/dino-head.png", 210, 190) 
dinoHead.x=665 
dinoHead.y=140 
dinoHead.rotation = 180 
dinoHead.headtype=3 

-------------------OTHER 

local nextButton=display.newImageRect("images/donebutton.png", 130, 130) 
nextButton.x=140 
nextButton.y=710 
group:insert(nextButton) 
nextButton.isVisible = false 

local nextText=display.newText("NEXT", 100,100, native.systemFontBold, 40) 
nextText.x=140 
nextText.y=710 
nextText:setTextColor (0,0,0) 
group:insert(nextText) 

function snapTo (event) 

    function makeVisible (event) 
      nextButton.isVisible = true 
    end 


    if event.phase == "began" then 
     event.target.markX = event.target.x -- store x location of object 
     event.target.markY = event.target.y -- store y location of object 
     torsoPlaced=event.target.torsoName 
    end 

    function addHeadsToGroup (event) 
     group:insert(robotHead) 
     group:insert(rabbitHead) 
     group:insert(dinoHead) 
    end 


    if event.phase == "moved" then 
     local x = (event.x - event.xStart) + event.target.markX 
     local y = (event.y - event.yStart) + event.target.markY 
     event.target.x, event.target.y = x, y -- move object based on calculations above 

     function demolish1 (event) 
      robotHead.x = 1000 
      robotHead.y = 900 
      robotHead.width = 10 
      robotHead.length = 10 
      robotHead.isVisible = false 
      group:insert(robotHead) 
      local staticRobot = display.newImageRect("images/robot-head.png", 195, 180) 
      staticRobot.x=137 
      staticRobot.y=130 
      staticRobot.rotation = 180 
      group:insert(staticRobot) 
     end 

     function demolish2 (event) 
      rabbitHead.x = 1000 
      rabbitHead.y = 900 
      rabbitHead.width = 10 
      rabbitHead.length = 10 
      rabbitHead.isVisible = false 
      group:insert(rabbitHead) 
      local staticRabbit = display.newImageRect("images/rabbit-head.png", 200, 180) 
      staticRabbit.x=387 
      staticRabbit.y=130 
      staticRabbit.rotation = 180 
      group:insert(staticRabbit) 
     end 

     function demolish3 (event) 
      dinoHead.x = 1000 
      dinoHead.y = 900 
      dinoHead.width = 10 
      dinoHead.length = 10 
      dinoHead.isVisible = false 
      group:insert(dinoHead) 
      local staticDino = display.newImageRect("images/dino-head.png", 210, 190) 
      staticDino.x=665 
      staticDino.y=140 
      staticDino.rotation = 180 
      group:insert(staticDino) 
     end 

     if (event.target.headtype == 1) then 
      event.target.width = 220 
      event.target.height = 200 
      if event.target.y > 400 then 
       event.target.isVisible = false 
       local newHead1 = display.newImageRect("images/robot-head.png",220, 200) 
       newHead1.x=150 
       newHead1.y=500 
       newHead1.rotation = 270 
      end 
      addHeadsToGroup() 
      makeVisible() 
      demolish2() 
      demolish3() 
     end 


     if (event.target.headtype == 2) then 
      event.target.width = 340 
      event.target.height = 240 
      if event.target.y > 400 then 
       event.target.isVisible = false 
       local newHead2 = display.newImageRect("images/rabbit-head.png",340, 240) 
       newHead2.x=140 
       newHead2.y=500 
       newHead2.rotation = 270 
      end 
      addHeadsToGroup() 
      makeVisible() 
      demolish1() 
      demolish3() 
     end 

     if (event.target.headtype == 3) then 
      event.target.width = 280 
      event.target.height = 250 
      if event.target.y > 400 then 
       event.target.isVisible = false 
       local newHead3 = display.newImageRect("images/dino-head.png",280, 250) 
       newHead3.x=140 
       newHead3.y=540 
       newHead3.rotation = 270 
      end 
      addHeadsToGroup() 
      makeVisible() 
      demolish1() 
      demolish2() 
     end 


     local function nextButtonClicked2 (event) 

      if event.phase=="ended" then 
       function sceneChange (event) 
        composer.gotoScene("mainbuildscene") 
        print("jererefs") 
       end 

       print("egsijegij") 
       sceneChange() 

      end 
     end 
     nextButton:addEventListener("touch", nextButtonClicked2) 
    end 
end--end of snapTo 


robotHead:addEventListener("touch", snapTo) 
rabbitHead:addEventListener("touch", snapTo) 
dinoHead:addEventListener("touch", snapTo) 






end -- end 


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

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

if (phase == "will") then 
    -- Called when the scene is still off screen (but is about to come on screen). 
elseif (phase == "did") then 
    -- Called when the scene is now on screen. 
    -- Insert code here to make the scene come alive. 
    -- Example: start timers, begin animation, play audio, etc. 
end 
end 


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

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

if (phase == "will") then 
    -- Called when the scene is on screen (but is about to go off screen). 
    -- Insert code here to "pause" the scene. 
    -- Example: stop timers, stop animation, stop audio, etc. 
elseif (phase == "did") then 
    -- Called immediately after scene goes off screen. 
end 
end 


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

local sceneGroup = self.view 

-- Called prior to the removal of scene's view ("sceneGroup"). 
-- Insert code here to clean up the scene. 
-- Example: remove display objects, save state, etc. 
end 


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

-- Listener setup 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 

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

return scene 

這是我的mainbuildscene文件,我試圖加載的文件。它正在加載,但前一幕不會消失。

local composer = require("composer") 

local scene = composer.newScene() 

-- ----------------------------------------------------------------------------------------------------------------- 
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. 
-- ----------------------------------------------------------------------------------------------------------------- 

-- local forward references should go here 

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


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



    local sceneGroup = self.view 
    function removeTheScene (event) 
     print("this is not working, unless it is...?") 
     composer.removeScene("buildscene2") 

    end 
    removeTheScene() 
-- Initialize the scene here. 
-- Example: add display objects to "sceneGroup", add touch listeners, etc. 
local group=display.newGroup() 
-- 
--  
     local picture=display.newImageRect("images/bee-torso.png", 100, 100) 
     picture.x=display.contentCenterX 
     picture.y=display.contentCenterY 
     group:insert(picture) 





end 


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

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

if (phase == "will") then 

    -- Called when the scene is still off screen (but is about to come on screen). 
elseif (phase == "did") then 
    -- Called when the scene is now on screen. 
    -- Insert code here to make the scene come alive. 
    -- Example: start timers, begin animation, play audio, etc. 
end 
end 


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

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

if (phase == "will") then 
    -- Called when the scene is on screen (but is about to go off screen). 
    -- Insert code here to "pause" the scene. 
    -- Example: stop timers, stop animation, stop audio, etc. 
elseif (phase == "did") then 
    -- Called immediately after scene goes off screen. 
end 
end 


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

local sceneGroup = self.view 

-- Called prior to the removal of scene's view ("sceneGroup"). 
-- Insert code here to clean up the scene. 
-- Example: remove display objects, save state, etc. 
end 


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

-- Listener setup 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 

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

return scene 

請讓我知道我做錯了什麼,或給我任何建議,你可以。謝謝閱讀。

回答

1

您必須將它們添加到 「sceneGroup」。您將所有對象添加到自己的組中,但它們應添加到「sceneGroup」中。

在buildscene2.lua刪除這些行:

local group = display.newGroup() 
composer.removeScene("thirdscene") 

並確保你添加所有的顯示對象到sceneGroup,而不是組:

更改此group:insert(itemFrame1)這個sceneGroup:insert(itemFrame1)但所有您的顯示對象和所有場景。

如果您打算重複使用場景(即能夠前後移動),則不需要使用removeScene,因爲電暈處理屬於場景的所有顯示對象,即當您從場景A到場景乙電暈將自動從屏幕移開屬於場景甲的顯示對象並移動屬於場景乙屏幕上的顯示對象。

相關問題