composer.removeScene()无法正常工作,其他显示对象不会消失

时间:2015-12-27 03:21:41

标签: lua corona

我正在使用Corona SDK在lua中开发应用程序。

我正在尝试从我的buildscene2转到我的mainbuildscene,并且我成功调用了新场景,但来自buildscene2的对象没有被删除。我在mainbuildscene.lua文件中添加了删除前一个场景的代码。我尝试在scene: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 个答案:

答案 0 :(得分:1)

您必须将它们添加到" sceneGroup"。您正在将所有对象添加到您自己的组中,但应将它们添加到" sceneGroup"。

在buildscene2.lua中删除以下行:

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

并确保将所有显示对象添加到sceneGroup中,而不是组:

将此group:insert(itemFrame1)更改为此sceneGroup:insert(itemFrame1),但适用于所有显示对象和所有场景。

如果您计划重复使用场景(即能够前后移动),则不需要使用removeScene,因为Corona会处理属于场景的所有显示对象即,从场景A到场景B时,Corona将自动移开属于场景A 的显示对象>,并将属于场景B的显示对象移动到屏幕。