Properly remove a scrollView

Hi guys, In my game I have:

local function goToEnv(event) selectedSection = 5 transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then envIsActive = true rightArrow.isVisible = false local function scrollListener( event ) local phase = event.phase local direction = event.direction return true end -- Create a ScrollView local scrollView = widget.newScrollView { left = display.contentWidth/2-130, top = display.contentHeight/2-165, width = 440, height = 300, bottomPadding = 10, id = "onBottom", backgroundColor = { 0, 0, 0, 0 }, horizontalScrollDisabled = true, verticalScrollDisabled = false, listener = scrollListener, } end end local function goToBonus(event) transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then selectedSection = 2 rightArrow.isVisible = true leftArrow.isVisible = true if(envIsActive==true) then scrollView:removeSelf() scrollView = nil envIsActive = false end end end

As you can see, inside the “goToEnv” function I create a scrollView, while inside the “goToBonus” function I remove it. Or at list I would remove it, since I got the error “Attempt to index global ‘scrollView’ (a nil value)”.

 

Is there something I can do?

 

 

Thanks a lot. 

did you tried:

if(envIsActive==true) then if scrollView then scrollView:removeSelf() scrollView = nil end envIsActive = false end or: if(envIsActive==true) then display.remove(scrollView) -- it have built in check if exists envIsActive = false end

Yes I tried. It didn’t give the error, but the widget still exists.

Yes, you didnt declare scrollView outside goToEnv function

local scrollView local function goToEnv(event) selectedSection = 5 transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then envIsActive = true rightArrow.isVisible = false local function scrollListener( event ) local phase = event.phase local direction = event.direction return true end -- Create a ScrollView scrollView = widget.newScrollView { left = display.contentWidth/2-130, top = display.contentHeight/2-165, width = 440, height = 300, bottomPadding = 10, id = "onBottom", backgroundColor = { 0, 0, 0, 0 }, horizontalScrollDisabled = true, verticalScrollDisabled = false, listener = scrollListener, } end end local function goToBonus(event) transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then selectedSection = 2 rightArrow.isVisible = true leftArrow.isVisible = true if(envIsActive==true) then scrollView:removeSelf() scrollView = nil envIsActive = false end end end

did you tried:

if(envIsActive==true) then if scrollView then scrollView:removeSelf() scrollView = nil end envIsActive = false end or: if(envIsActive==true) then display.remove(scrollView) -- it have built in check if exists envIsActive = false end

Yes I tried. It didn’t give the error, but the widget still exists.

Yes, you didnt declare scrollView outside goToEnv function

local scrollView local function goToEnv(event) selectedSection = 5 transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then envIsActive = true rightArrow.isVisible = false local function scrollListener( event ) local phase = event.phase local direction = event.direction return true end -- Create a ScrollView scrollView = widget.newScrollView { left = display.contentWidth/2-130, top = display.contentHeight/2-165, width = 440, height = 300, bottomPadding = 10, id = "onBottom", backgroundColor = { 0, 0, 0, 0 }, horizontalScrollDisabled = true, verticalScrollDisabled = false, listener = scrollListener, } end end local function goToBonus(event) transition.to( storeObjects[17], { time=400, alpha = 1} ) if(event.phase=="ended")then selectedSection = 2 rightArrow.isVisible = true leftArrow.isVisible = true if(envIsActive==true) then scrollView:removeSelf() scrollView = nil envIsActive = false end end end