好的,这个问题有很多答案,但我似乎无法让它发挥作用。我的按钮是在函数 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
我正在尝试删除对象,以免影响下一个场景,但似乎无法做到。