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