Clear last scene

Hi, im having an issue with scene interaction.

Example: I did a scrollview on scene 1 (called view.lua) but when in my tab bar press scene 2 the scrollview of the scene 1 still there too, and on scene 3, etc…, it appears on all my scenes that scrollview, how i can do for only shows on scene 1:

----------------------------------------------------------------------------------------- -- -- view1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) -- Scrollview listeners local function scrollListener( event ) local phase = event.phase local direction = event.direction -- Limits if event.limitReached then if "up" == direction then print( "Limite alcanzado" ) else if "down" == direction then print( "Limite alcanzado" ) end end return true end end -- Create scrollview local scrollView = widget.newScrollView { left = 21, top = 26, width = 280, height = 350, topPadding = 50, bottomPadding = 50, horitzontalScrollDisabled = true, verticalScrollDisabled = false, listener = scrollListener, --hideBackground = true, } function scene:create( event ) local sceneGroup = self.view -- create some text local title = display.newText( "Cartas", display.contentCenterX, 0, native.systemFont, 32 ) title:setFillColor( 1 ) -- black local newTextParams = { text = "", x = display.contentCenterX + 10, y = title.y + 215, width = 310, height = 310, font = native.systemFont, fontSize = 14, align = "center" } local summary = display.newText( newTextParams ) summary:setFillColor( 0 ) -- black -- all objects must be added to group (e.g. self.view) sceneGroup:insert( title ) sceneGroup:insert( summary ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

Im using default template and editing it, i dont know how many of this default i can remove (i tried to remove everything but gives me too many errors.

Thanks.

I solved doing this, im not sure if its the correct way:

(If not, please post the correct way for clean and optimize my code)

on scene:hide

function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then local function removeScrollView(event) scrollView:removeSelf() scrollView = nil end end end

You should probably put the scrollView inside of scene:create() then afterwards do:

sceneGroup:insert( scrollView)

Then if you need to insert things into the scrollView outside of the scene:create() function, then near the top of the scene do:

local scrollView

And in the scene:create() take the “local” off of the scrollView = widget.newScrollView() line. That way you can access the scrollView throughout the entire scene. If you do this you won’t need to remove it manually in scene:hide().

Rob

I solved doing this, im not sure if its the correct way:

(If not, please post the correct way for clean and optimize my code)

on scene:hide

function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then elseif phase == "did" then local function removeScrollView(event) scrollView:removeSelf() scrollView = nil end end end

You should probably put the scrollView inside of scene:create() then afterwards do:

sceneGroup:insert( scrollView)

Then if you need to insert things into the scrollView outside of the scene:create() function, then near the top of the scene do:

local scrollView

And in the scene:create() take the “local” off of the scrollView = widget.newScrollView() line. That way you can access the scrollView throughout the entire scene. If you do this you won’t need to remove it manually in scene:hide().

Rob