Too many (200) local variables

I have encountered the problem of too many local variables again. This is occurring in my Screen1.lua file and I did make the effort to put all my variables into an array/object like this:

  
local splashVariables = {}  
  
splashVariables["boolExtraFeatures"] = true  
splashVariables["numBallOptionStage"] = 1  
splashVariables["numTipsPage"] = 1  
splashVariables["numInstructionsPage"] = 1  
splashVariables["numAchievementsPage"] = 1  
splashVariables["optionsDisplayed"] = false  

The problem is however not with variables as such but instead with functions and some images I need to have a reference to. I can’t believe that there are 200 of these but guess there is as there is over 4000 lines of code in this lua file

e.g

  
local function cancelTransitions()  
  
 Runtime:removeEventListener("enterFrame", animateScroller)  
  
 if splashPanelTrans ~= nil then  
  
 transition.cancel(splashPanelTrans)  
  
 end  
  
end  
  
  
local fadeBg = display.newImage("images/fade.png", -1000, -1000)  
  
localGroup:insert(fadeBg)  
  
function displayFadeBg()  
  
 fadeBg.alpha = 0  
 fadeBg.x = 160  
 fadeBg.y = 240  
  
 transition.to( fadeBg, { time=250, alpha=1} )  
  
 fadeBg:addEventListener("touch", clickFadeBg)  
  
end  
  

Has anyone any suggestions how to reduce the number of functions or image references. Can I do something like?

[code]

splashVariables[“fadeBg”] = display.newImage(“images/fade.png”, -1000, -1000)

[/code] [import]uid: 7863 topic_id: 6172 reply_id: 306172[/import]

Can I do something like?

yes

Incidentally, with the way you’ve written those functions I’m guessing you have a separate function for each image, which is a little insane. Write functions that operate generically on whatever image you pass in, and then pass the image in when calling the function. [import]uid: 12108 topic_id: 6172 reply_id: 21195[/import]

Thanks jhocking

I was sure my code was a bit insane - just not quite sure how to restructure it to be honest

For example every time I have a button I have all the following code. Would appeciate suggestions how to make a more generic button code solution

[code]


– Ball Options Back Button


local resumeOptionsBackButton = uiNew.newButton{
default = “images/backDefault.png”,
over = “images/backHighlight.png”,
hit = true,
hitWidthExtra = 140,
hitHeightExtra = 10

}

resumeOptionsBackButton.x = 156; resumeOptionsBackButton.y = 406

local function clickResumeOptionsBackButton ( event )

local buttonVariables = {}

buttonVariables[“self”] = event.target

–local self = event.target

if event.phase == “ended” then

buttonVariables[“bounds”] = buttonVariables[“self”].stageBounds
buttonVariables[“x”] = event.x
buttonVariables[“y”] = event.y

buttonVariables[“isWithinBounds”] = buttonVariables[“bounds”].xMin <= buttonVariables[“x”] and buttonVariables[“bounds”].xMax >= buttonVariables[“x”] and buttonVariables[“bounds”].yMin <= buttonVariables[“y”] and buttonVariables[“bounds”].yMax >= buttonVariables[“y”]

if buttonVariables[“isWithinBounds”] then

removeResumeOptions()

end

end

end

resumeOptionsBackButton:addEventListener(“touch”,clickResumeOptionsBackButton)

ballOptionsPanel:insert(resumeOptionsBackButton)
[/code] [import]uid: 7863 topic_id: 6172 reply_id: 21210[/import]

Well first off, I don’t believe that you actually have that code for every single button. Because that code is for a back button, so that would mean all of your buttons are back buttons.

Just going along with what you said however, then you could simply reuse that function with every button, since there’s nothing in that function specific to that one image. I mean, you already use event.target instead of resumeOptionsBackButton, which is what you should be doing to keep the function generic to all buttons and not just the one.

In other words, you only have to write the function once and then use the same function in every event listener:

backButton[1]:addEventListener("touch",clickResumeOptionsBackButton)  
backButton[2]:addEventListener("touch",clickResumeOptionsBackButton)  
backButton[3]:addEventListener("touch",clickResumeOptionsBackButton)  
etc.  

Now that said, there’s a lot of ugliness within the function. In particular, I don’t know why you made that buttonVariables table. Instead of making event.x an entry in the table and then using the table, just use event.x directly:

local withinBounds = event.target.contentBounds.xMin \< event.x and etc  

Incidentally, stageBounds is deprecated:
http://developer.anscamobile.com/reference/index/objectcontentbounds [import]uid: 12108 topic_id: 6172 reply_id: 21224[/import]

Thanks jhocking - I really appreciate the guidance on this.

Amending my code as follows, I still have 2 locals declared.

You are correct that I do not have lots of back buttons. All my buttons are different buttons so I assume each needs a separate local declaration? Or do I create a table of all by buttons?

Also because each button has different function calls, I am not sure if I could just have one generic function to deal with all buttons unless I was able to pass to the button the functions that need to be called when the button is pressed.

  
 -- ------------------------  
 -- Ball Options Back Button  
 -- ------------------------  
  
 local resumeOptionsBackButton = uiNew.newButton{  
 default = "images/backDefault.png",  
 over = "images/backHighlight.png",  
 hit = true,  
 hitWidthExtra = 140,  
 hitHeightExtra = 10  
  
 }  
  
  
 resumeOptionsBackButton.x = 156; resumeOptionsBackButton.y = 406  
  
 local function clickResumeOptionsBackButton ( event )  
  
 if event.phase == "ended" then  
  
 local withinBounds = event.target.contentBounds.xMin \<= event.x and event.target.contentBounds.xMax \>= event.x and event.target.contentBounds.yMin \<= event.y and event.target.contentBounds.yMax \>= event.y  
  
  
 if withinBounds then  
  
 removeResumeOptions()  
  
 end  
  
 end   
  
  
 end  
  
  
 resumeOptionsBackButton:addEventListener("touch",clickResumeOptionsBackButton)  
  
 ballOptionsPanel:insert(resumeOptionsBackButton)  

Many thanks for the help

Paul [import]uid: 7863 topic_id: 6172 reply_id: 21235[/import]

This appears to work and reduces each button by one local declaration

  
local splashButtons = {}  
  
splashButtons["resumeOptionsBackButton"] = uiNew.newButton{  
 default = "images/backDefault.png",  
 over = "images/backHighlight.png",  
 hit = true,  
 hitWidthExtra = 140,  
 hitHeightExtra = 10  
  
 }  
  
  
 splashButtons["resumeOptionsBackButton"].x = 156; splashButtons["resumeOptionsBackButton"].y = 406  
  
 local function clickResumeOptionsBackButton ( event )  
  
 if event.phase == "ended" then  
  
 local withinBounds = event.target.contentBounds.xMin \<= event.x and event.target.contentBounds.xMax \>= event.x and event.target.contentBounds.yMin \<= event.y and event.target.contentBounds.yMax \>= event.y  
  
  
 if withinBounds then  
  
 removeResumeOptions()  
  
 end  
  
 end   
  
  
 end  
  
  
 splashButtons["resumeOptionsBackButton"]:addEventListener("touch",clickResumeOptionsBackButton)  
  
 resumePanel:insert(splashButtons["resumeOptionsBackButton"])  
  

[import]uid: 7863 topic_id: 6172 reply_id: 21262[/import]