Hi,
I’m new to Lua but am an experienced programmer.
I seem to be having trouble with variable scope, although the issue might be something else.
Some code snippets are as follows:
function chooseTeams()
chooseYourTeamHeading = display.newText(“Choose Your Team”, 0, 50, 0, 0, native.systemFontBold, 18)
chooseTeamType = “player”
chooseTeam()
end
function chooseTeam()
local bangladeshFlag = display.newImage(“flags_bangladesh.png”, 0, 90)
bangladeshFlag:addEventListener(“touch”, selectBangladeshTeam)
end
function selectBangladeshTeam()
if chooseTeamType == “player” then playerTeam = “Bangladesh”
else computerTeam = teamName
end
finishSelectPlayerTeam()
end
function finishSelectPlayerTeam()
chooseYourTeamHeading:removeSelf()
chooseYourTeamHeading = nil
end
So when the user clicks on the Bangladesh flag, finishSelectPlayerTeam() gets called, and the simulator comes up with this error
"Attempt to index global ‘chooseYourTeamHeading’ (a nil value). I thought this was global because it wasn’t declared local, and therefore should be accessible?
On another matter, is it good practise to clean up all objects like this or is it not that important?
And finally, with the call to the event listener, do I need to setup a different function for each flag that I create? Can I not have the one function that takes a parameter? eg
bangladeshFlag:addEventListener(“touch”, selectBangladeshTeam)
might become something like
bangladeshFlag:addEventListener(“touch”, selectTeam(“Bangladesh”))
thanks for your help!
Daniel