trouble reaching a scrollview created in createScene

I’m probably missing something basic, but I’m stumped.  I have created a scrollView in createScene, then when in enterScene I get the error

attempt to index global ‘scrollView’ (a nil value)

function scene:createScene( event )   local group = self.view   local scrollView = widget.newScrollView     {        left = 0,        top = 100,        width = display.contentWidth,        height = display.contentHeight - 320,        scrollWidth = display.contentWidth,        friction = 0.95,        listener = displayScrollListener,        horizontalScrollDisabled = true,        isBounceEnabled = true,     }   group:insert( scrollView ) end  

then

function scene:enterScene( event )     scrollView:scrollToPosition                 {                     x = 0,                     y = myGlobals.scrollToPosition,                     time = 800                 } end  

gets

attempt to index global ‘scrollView’ (a nil value)

how can I target/reference the scrollView that was created earlier when the scene was preloaded?

Thanks!

local scrollView…

Hint hint… take out local and it should work. 

The way you set it the scrollView is limited to the context of createScene.

@ksan has identified the problem.  Its a term in programming called “Scope”.  When you declare a variable or function as “local” its only visible within the block it’s defined in.  In this case, your scrollView is only visible to the createScene() function.  It make it visible to the entire scene module, you would put a:

local scrollView

at the top of the module outside of any function.  If you just simply leave the local off, it will become a global across the whole app.  This may seem like a good thing, but global’s are very dangerous unless you are very careful with them. 

Rob

@Rob, absolutely great suggestion. My bad for pushing @mfor1967 towards globals.

Awesome!  putting “local scrollView” at the top of that scene did the trick!

I had lots of globals flying around, but now that I’m preloading scenes while others are still showing things were getting jumbled together.  I’m using more local variables and passing them to functions.  Seeing that I can put them at the top and they stay within that module is giving me ideas…  

Thank you both! :slight_smile:

Great! Here’s a wonderful tutorial from Rob on the topic : http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

local scrollView…

Hint hint… take out local and it should work. 

The way you set it the scrollView is limited to the context of createScene.

@ksan has identified the problem.  Its a term in programming called “Scope”.  When you declare a variable or function as “local” its only visible within the block it’s defined in.  In this case, your scrollView is only visible to the createScene() function.  It make it visible to the entire scene module, you would put a:

local scrollView

at the top of the module outside of any function.  If you just simply leave the local off, it will become a global across the whole app.  This may seem like a good thing, but global’s are very dangerous unless you are very careful with them. 

Rob

@Rob, absolutely great suggestion. My bad for pushing @mfor1967 towards globals.

Awesome!  putting “local scrollView” at the top of that scene did the trick!

I had lots of globals flying around, but now that I’m preloading scenes while others are still showing things were getting jumbled together.  I’m using more local variables and passing them to functions.  Seeing that I can put them at the top and they stay within that module is giving me ideas…  

Thank you both! :slight_smile:

Great! Here’s a wonderful tutorial from Rob on the topic : http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/