この質問には多くの答えがありますが、うまくいかないようです。私のボタンは関数 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
次のシーンに影響を与えないように、オブジェクトを削除しようとしていますが、できないようです。