电晕故事板按钮未从下一个场景中正确删除

时间:2013-01-14 14:27:05

标签: lua corona corona-storyboard

好的,这个问题有很多答案,但我似乎无法让它发挥作用。我的按钮是在函数场景中创建的:createScene(事件),当我移动到下一个场景时,我正在尝试使用scenePurge并单独删除按钮,但它似乎不起作用。在下一个场景中,该按钮不可见,但如果我按下前一个场景上的按钮,它仍处于活动状态。删除这些对象的最佳方法是什么?

        local storyboard = require( "storyboard" )
        local scene = storyboard.newScene()

        --storyboard.removeScene( "login" )

---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

local image, image2


-- Called when the scene's view does not exist:
function scene:createScene( event )
    local widget = require( "widget" )

    local p1ButtonRelease = function( event )
    storyboard.gotoScene( "maths_dif", "slideLeft", 1000  )
        print("touch")
        return true 
    end

    local p2ButtonRelease = function( event )
    storyboard.gotoScene( "words", "slideLeft", 1000  )
        print("touch")
        return true


end

    local screenGroup = self.view

    image = display.newImage( "images/Background(640x480).png", -20 ,0 )
    image2 = display.newImage( "Logo_small.png", 170,0)
    --screenGroup:insert( image )

    print( "\n2: createScene event" )


    local p1Button = widget.newButton{
    default = "images/SinglePlayer_button.png",
    over = "images/SinglePlayer_button.png",
    --onPress = button1Press,
    onRelease = p1ButtonRelease,
    -- fontSize = 20,
    -- labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
    -- label = "+",
    -- emboss = true
    }


    local p2Button = widget.newButton{
    default = "images/2Player_button.png",
    over = "images/2Player_button.png",
    --onPress = button1Press,
    onRelease = p2ButtonRelease,
    -- fontSize = 20,
    -- labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
    -- label = "+",
    -- emboss = true
    }

    p1Button.x = 240; p1Button.y = 180
    p2Button.x = 240; p2Button.y = 260
end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )

    print( "2: enterScene event" )

    -- remove previous scene's view
    storyboard.purgeScene( "login" )
    display.remove(p1Button)
    -- -- Update Lua memory text display
    -- local showMem = function()
        image:addEventListener( "touch", image )
        -- text3.isVisible = true
        -- text2.text = text2.text .. collectgarbage("count")/1000 .. "MB"
        -- text2.x = display.contentWidth * 0.5
    -- end
    -- memTimer = timer.performWithDelay( 1000, showMem, 1 )
end


-- Called when scene is about to move offscreen:
function scene:exitScene()

    print( "2: exitScene event" )
    storyboard.removeScene("login")
    storyboard.removeScene("words")
    storyboard.removeScene("guess_word")
    storyboard.removeScene("maths_dif")
    storyboard.removeScene("scene1")
    display.remove(p1Button)
    p1Button = nil
    display.remove(p2Button)
    p2Button = nil
    -- remove touch listener for image
    image:removeEventListener( "touch", image )

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )

    print( "((destroying scene 2's view))" )
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

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

return scene

我正在尝试删除对象,这样就不会影响下一个场景,但似乎无法做到。

2 个答案:

答案 0 :(得分:0)

有人建议在每个场景的开头使用storyboard.removeAll(),但我个人发现的解决方案是不使用widget按钮释放功能来改变场景而是创建一个图像对象并给它一个EventListener,例如

    p1Button = display.newImage("images/SinglePlayer_button.png", 90, 140)
    p1Button.touch = onSceneTouch
p1Button:addEventListener("touch", p1Button)

    local function onSceneTouch( self, event )
    if event.phase == "began" then
        storyboard.gotoScene( "words", "slideLeft", 1000  )
        return true 
    end
end

然后我在exitScene()函数中删除了对象事件监听器。在我的下一个场景中,这些先前的按钮无效。

答案 1 :(得分:0)

您需要将按钮插入场景视图(组)中。在createScene()函数中,创建按钮后执行:

group:insert(p1Button)

然后你不必担心删除它们,故事板会为你做。