Wrong display object updating...

I am new to Lua so maybe I am missing something simple about object referencing.

I’m keeping an array of buttons so that I can fake a touch event when needed (the game involves showing a pattern). I have 12 buttons on screen but when I iterate through, only the 12th button ever shows the fake touch event. I have debugged to see if my array is populating (it is) and if the correct button was being triggered (it is). I am out of ideas…

-- array of buttons local lights = {}

This is my loop to populate the buttons and add them lights

-- iterating through an array to populate the buttons on screen for i=0,3 do for j=1,3 do tablePlace = tablePlace + 1 local xCoord = 0 local yCoord = 0 -- set the x coordinate if(j == 1) then xCoord = 60 elseif(j == 2) then xCoord = halfWidth else xCoord = display.contentWidth-60 end -- set the y coordinate yCoord = 85 + (i\*100) newLight = ui.newButton { defaultSrc = "button.png" , overSrc = "button-over.png" , x = xCoord, y = yCoord, defaultX = 45, defaultY = 45, overX = 45, overY = 45, onRelease = onReleaseLight, id = tablePlace } newLight.place = tostring( tablePlace ) myScreen:insert(newLight) -- add them to the array here lights[tablePlace] = newLight end end

The function iterates through the array and triggers the fake event through the timerListeners

-- Function to show the pattern (generate fake touch event) local function showLightPattern( group ) local delay = 1000 for i=1,level do print(tostring(i)..": #"..tostring(lightPattern[i])) local light = lights[lightPattern[i]] local turnOnLightClosure = function() return turnOnLightTimerListener( light ) end timer.performWithDelay( delay, turnOnLightClosure ) if(i \> 1) then local previousLight = lights[lightPattern[i-1]] local turnOffLightClosure = function() return turnOffLightTimerListener( previousLight ) end timer.performWithDelay( delay-500, turnOffLightClosure ) end -- Need to make the last light turn off! if(i == level) then local endOfLightsClosure = function() return turnOffLightTimerListener( light ) end timer.performWithDelay( delay+300, endOfLightsClosure ) end delay = delay + 800 end end

Here are the timerListeners, the :showOverSrc() and :hideOverSrc() are custom functions I created in ui.lua from the UI Library. They simply have the line 

over.isVisible = true/false  

 in them. Like I said before the 12th button shows the turning on and off of the button but it should be jumping button to button. Setting the text works correctly and jumps from button to button

local function turnOnLightTimerListener( light ) print( "Turn on "..tostring( light.place )) light:showOverSrc() light:setText( tostring( light.place ) ) end local function turnOffLightTimerListener( previousLight ) print( "Turn off "..tostring( previousLight.place )) previousLight:hideOverSrc() previousLight:setText( "" ) end

Ended up answering my own question…

in my ui.lua file:

function button:showOverSrc() -- over.isVisible = true --\> This only updates the last-made object button[2].isVisible = true --\> This updates the correct button by referencing the over variable in the button group end function button:hideOverSrc() -- over.isVisible = false --\> This only updates the last-made object button[2].isVisible = false --\> This updates the correct button by referencing the over variable in the button group end

I guess the reference is deferent when you actually use button, versus just referencing the over variable. Who knows. If you have any questions on this let me know

Ended up answering my own question…

in my ui.lua file:

function button:showOverSrc() -- over.isVisible = true --\> This only updates the last-made object button[2].isVisible = true --\> This updates the correct button by referencing the over variable in the button group end function button:hideOverSrc() -- over.isVisible = false --\> This only updates the last-made object button[2].isVisible = false --\> This updates the correct button by referencing the over variable in the button group end

I guess the reference is deferent when you actually use button, versus just referencing the over variable. Who knows. If you have any questions on this let me know