Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

touch listener for table[i]
Started by dodi_games Jan 17 2019 04:15 PM

3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 344 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,263 posts
  • Enterprise

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



[TOPIC: post.html]
#3

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 344 posts
  • Corona SDK

@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 

    --level button status
    if ( i <= unlockedLevels ) then
        levelBtns[i]:addEventListener("touch", handleLevelSelect)
        levelBtns[i]:setFillColor( 254/255, 133/255, 54/255 )
    else
        levelBtns[i]:setFillColor( 150/255, 150/255, 150/255 )
    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



[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 344 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]