0

好的,这个问题有很多答案,但我似乎无法让它发挥作用。我的按钮是在函数 scene:createScene( event ) 中创建的,当我进入下一个场景时,我正在尝试 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

我正在尝试删除对象,以免影响下一个场景,但似乎无法做到。

4

2 回答 2

0

有人建议在每个场景开始时使用storyboard.removeAll(),但我个人发现的解决方案不是使用小部件按钮释放功能来改变场景,而是创建一个图像对象并给它一个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() 函数中删除对象事件侦听器。在我的下一个场景中,这些先前的按钮无效。

于 2013-01-16T10:02:43.630 回答
0

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

group:insert(p1Button)

然后您不必担心删除它们,情节提要会为您完成。

于 2013-01-20T22:00:00.750 回答