Help me wrap my head around this

In this code for a credits screen:

[lua]local creditsbackground = display.newImageRect(“Credits.png”, 480, 320)
creditsbackground.x = display.contentWidth / 2; creditsbackground.y = display.contentHeight / 2
creditsitems:insert( creditsbackground )

local backRelease = function(event)
media.playEventSound( buttonsound )
creditsitems:removeSelf() --this removes the entire scene and the next line goes back to menu
menu()
end

local back = ui.newButton{
default = “back.png”,
over = “back2.png”,
onRelease = backRelease
}
back.x = 223; back.y = 288
creditsitems:insert( back )[/lua]

calling creditsitems:removeSelf() removes everything, even the back button that was added after the backRelease function (which is the desired behavior).

However if I try to manipulate back (say back.alpha = 0) inside the backRelease function I get the attempted to index a nil global value error, since apparently local back hasn’t been declared yet.

HOWEVER if I do the same but instead of calling back directly I do creditsitems[2].alpha = 0 it works.

So how can “back” be in the creditsitems group without having been declared? Right now I’m using the workaround of calling every item by its parent group number, but I’d like to know why this happens. Thanks.
[import]uid: 10835 topic_id: 4296 reply_id: 304296[/import]

you havent defined “back” before your backRelease function. it doesnt know what “back” is. do this…

[lua]local creditsbackground = display.newImageRect(“Credits.png”, 480, 320)
creditsbackground.x = display.contentWidth / 2; creditsbackground.y = display.contentHeight / 2
creditsitems:insert( creditsbackground )

local back – forward reference

local backRelease = function(event)
media.playEventSound( buttonsound )
back.alpha = 0 – i know what “back” is now!
creditsitems:removeSelf() --this removes the entire scene and the next line goes back to menu
menu()
end

back = ui.newButton{
default = “back.png”,
over = “back2.png”,
onRelease = backRelease
}
back.x = 223; back.y = 288
creditsitems:insert( back )[/lua]

or this

[lua]local creditsbackground = display.newImageRect(“Credits.png”, 480, 320)
creditsbackground.x = display.contentWidth / 2; creditsbackground.y = display.contentHeight / 2
creditsitems:insert( creditsbackground )

local backRelease – forward reference to function

local back = ui.newButton{
default = “back.png”,
over = “back2.png”,
onRelease = backRelease
}

back.x = 223; back.y = 288
creditsitems:insert( back )

backRelease = function(event) – or you could say function backRelease(event)
media.playEventSound( buttonsound )
creditsitems:removeSelf() --this removes the entire scene and the next line goes back to
menu
back.alpha=0
menu()
end[/lua] [import]uid: 6645 topic_id: 4296 reply_id: 13372[/import]

Yes I kinda understood it was because I hadn’t defined “back”, but good tip on how to fix it. However my question was more aimed at why, even though I haven’t defined “back”, the second child of credititems IS “back”. [import]uid: 10835 topic_id: 4296 reply_id: 13374[/import]

This is the part I was having trouble understanding: “and by the time it needs to read what’s actually in there you’ve inserted back.”

But I think I get it now. Thanks for the explanation. [import]uid: 10835 topic_id: 4296 reply_id: 13376[/import]

because the first item you inserted is creditsBackground and the second item you inserted is back

[lua]creditsitems:insert( creditsbackground )
creditsitems:insert( back )[/lua]

backRelease can see “creditsitems” because you defined it in advance. and by the time it needs to read what’s actually in there you’ve inserted your “back” object into the “creditsitems” object. it’s just at the time you *defined* your function you hadn’t told it what the variable “back” actually pointed to, which is why “back” is nil there

i guess the best way to think of it is that when your code is defined you’re setting up the pointers, and at runtime you’re filling in the values. lua knows creditsitems is an “array” so creditsitems[2] is fine. (as presumably would be creditsitems[2].whatever as it’s just expecting dynamic properties). however it has no reference as to what “back” is at that point. your variable doesnt exist on it’s own [import]uid: 6645 topic_id: 4296 reply_id: 13375[/import]