touch listener for table[i]

Hi guys! I am trying to do my level selector scene and I have encountered several problems because my level button are inserted in a scrollview. Following the " Building a Level Selection Scene Tutorial " I decided to make a change that is more suited to my needs, but, I can not add the listener for only the levels that the player has unlocked.

LISTENER:

-- Button handler to go to the selected level local function handleLevelSelect( event )   if ( event.phase == "moved" ) then       dy = math.abs( event.y - event.yStart )       if ( dy \> 5 ) then         scrollView:takeFocus( event )       end   elseif ( event.phase == "ended" ) then       print( "button release" )               -- Go to the game scene       local options = { effect = "crossFade", time = 200 }       composer.gotoScene( "levels.level"..event.target.id, options )   end end

CODE:

&nbsp; local xOffset = 154 &nbsp; local yOffset = 2103 &nbsp; local cellCount = 1 &nbsp; local levelBtns = {} &nbsp; local maxLevels = 50 &nbsp; local unlockedLevels = (gameSettings.progressNumber or 1) &nbsp; for i = 1, maxLevels do &nbsp;&nbsp;&nbsp; levelBtns[i] = display.newRoundedRect( 0, 0, 75, 75, 8 ) &nbsp;&nbsp;&nbsp; levelBtns[i].id = tostring( i ) &nbsp;&nbsp;&nbsp; levelBtns[i].strokeWidth = 2 &nbsp;&nbsp;&nbsp; levelBtns[i]:setStrokeColor( 115/255, 115/255, 115/255 ) &nbsp;&nbsp;&nbsp; for i = 1, unlockedLevels do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:addEventListener("touch", handleLevelSelect) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; -- Position the button in the grid and add it to the scrollView &nbsp;&nbsp;&nbsp; levelBtns[i].x = xOffset &nbsp;&nbsp;&nbsp; levelBtns[i].y = yOffset &nbsp;&nbsp;&nbsp; scrollView:insert( levelBtns[i] ) &nbsp;&nbsp;&nbsp; xOffset = xOffset + 115 &nbsp;&nbsp;&nbsp; cellCount = cellCount + 1 &nbsp;&nbsp;&nbsp; if ( cellCount \> 5 ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cellCount = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xOffset = 154 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yOffset = yOffset + 100 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; --level button status &nbsp;&nbsp;&nbsp; if ( i \<= unlockedLevels ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 254/255, 133/255, 54/255 ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 150/255, 150/255, 150/255 ) &nbsp;&nbsp;&nbsp; end &nbsp; end

what I’m doing wrong?

Thanks in advance

DoDi

Basically, you have a loop that builds each button but inside you have another loop that’s repeatedly adding listeners. At the bottom of the loop you have an if statement that sets the color of the button. Is that working?

It seems to me you would want to replace this:

 for i = 1, unlockedLevels do levelBtns[i]:addEventListener("touch", handleLevelSelect) end

with

 if ( i \<= unlockedLevels ) then levelBtns[i]:addEventListener("touch", handleLevelSelect) end

Rob

@Rob

you have an if statement that sets the color of the button. Is that working?

yes!!!

I try to add the listener in the 

&nbsp;&nbsp;&nbsp; --level button status &nbsp;&nbsp;&nbsp; if ( i \<= unlockedLevels ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:addEventListener("touch", handleLevelSelect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 254/255, 133/255, 54/255 ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 150/255, 150/255, 150/255 ) &nbsp;&nbsp;&nbsp; end

and do not work  :wacko:

it does not matter where I place the listener, I can not see the print(“button release”) on the console when I touch a button

I found the problem! I needed to return true at the end of the listener.  

Thanks @Rob, when you talked to me about moving the listener, Its help me to make tests to realized where the error was.

Basically, you have a loop that builds each button but inside you have another loop that’s repeatedly adding listeners. At the bottom of the loop you have an if statement that sets the color of the button. Is that working?

It seems to me you would want to replace this:

 for i = 1, unlockedLevels do levelBtns[i]:addEventListener("touch", handleLevelSelect) end

with

 if ( i \<= unlockedLevels ) then levelBtns[i]:addEventListener("touch", handleLevelSelect) end

Rob

@Rob

you have an if statement that sets the color of the button. Is that working?

yes!!!

I try to add the listener in the 

&nbsp;&nbsp;&nbsp; --level button status &nbsp;&nbsp;&nbsp; if ( i \<= unlockedLevels ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:addEventListener("touch", handleLevelSelect) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 254/255, 133/255, 54/255 ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; levelBtns[i]:setFillColor( 150/255, 150/255, 150/255 ) &nbsp;&nbsp;&nbsp; end

and do not work  :wacko:

it does not matter where I place the listener, I can not see the print(“button release”) on the console when I touch a button

I found the problem! I needed to return true at the end of the listener.  

Thanks @Rob, when you talked to me about moving the listener, Its help me to make tests to realized where the error was.