Wiget Button Fucntion in Scene View

Hi Folks,
 
My apologies in advance if this is an easy solution and I am being really dosile!
 
I am simply trying to implement a user controlled score counter, using a few widget buttons. I have the button widgets in the scene:create section and the fucntions in the scene accessible code area. Nothing is happening :frowning:
Any help will be greatly appreciated.
 
Code:

local function scoreIncrease( string )
    
  if ( string == “minusP1” ) then
    print(“pressed = P1-”)
    if ( scoreP1 > 0 ) then
      scoreP1 = scoreP1 - 1
    end    
    
  elseif ( string == “plusP1” ) then    
    print(“pressed = P1+”)
    if ( scoreP1 < 10 ) then
      scoreP1 = scoreP1 + 1
    end
    
  elseif ( string == “minusP2” ) then    
    print(“pressed = P2-”)
    if ( scoreP2 > 0 ) then
      scoreP2 = scoreP2 - 1
    end
    
  elseif ( string == “plusP2” ) then    
    print(“pressed = P2+”)
    if ( scoreP2 < 10 ) then
      scoreP2 = scoreP2 + 1
    end
  end  
end

function scene:create( event )
    local sceneGroup = self.view
    
    minusP1= widget.newButton{
    label="-",
    fontSize=130,
    labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
    labelXOffset = -2,  
    labelYOffset = -14,
    defaultFile=“content/btn1.png”,
    overFile=“content/btn1Over.png”,
    width=200, height=200,
    onRelease = scoreIncrease( “minusP1” )
  }
  minusP1.x = display.contentCenterX - 175
  minusP1.y = display.contentCenterY + 25
 
  plusP1= widget.newButton{
    label="+",
    fontSize=130,
    labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
    labelXOffset = -2,  
    labelYOffset = -4,
    defaultFile=“content/btn1.png”,
    overFile=“content/btn1Over.png”,
    width=200, height=200,
    onRelease = scoreIncrease( “plusP1” )
  }
  plusP1.x = display.contentCenterX + 175
  plusP1.y = display.contentCenterY + 25
   
  minusP2= widget.newButton{
    label="-",
    fontSize=130,
    labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
    labelXOffset = -2,  
    labelYOffset = -14,
    defaultFile=“content/btn2.png”,
    overFile=“content/btn2Over.png”,
    width=200, height=200,
    onRelease = scoreIncrease( “minusP2” )
  }
  minusP2.x = display.contentCenterX - 175
  minusP2.y = display.contentCenterY + 225
 
  plusP2= widget.newButton{
    label="+",
    fontSize=130,
    labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
    labelXOffset = -2,  
    labelYOffset = -4,
    defaultFile=“content/btn2.png”,
    overFile=“content/btn2Over.png”,
    width=200, height=200,
    onRelease = scoreIncrease( “plusP2” )
  }
  plusP2.x = display.contentCenterX + 175
  plusP2.y = display.contentCenterY + 225

All the best,

Alex

So nothing is printed as well?

No, I’m not getting anything back… :frowning:

You have to pass function not its call like 

onRelease = scoreIncrease

In scoreIncrease listener you will get event which contains all necessary information. You can find out what button was pressed by simply checking event.target:getLabel(), for instance 

[lua] local function scoreIncrease( event)

 if ( event.target:getLabel() == “-” ) then
    print(“pressed = P1-”)
    if ( scoreP1 > 0 ) then
      scoreP1 = scoreP1 - 1

    end [/lua]   

Thank you Bektur, you have saved me!

I also now have a much better understanding of what is going on.

In the end I assigned each widget an ‘id’ (as they had shared label values), and then completed the fucntion using:

if ( event.target.id == “minusP1” ) then

etc…

I very much appreciate your help with this, I will hopefully resume making fast progress now.

All the best,

Alex

So nothing is printed as well?

No, I’m not getting anything back… :frowning:

You have to pass function not its call like 

onRelease = scoreIncrease

In scoreIncrease listener you will get event which contains all necessary information. You can find out what button was pressed by simply checking event.target:getLabel(), for instance 

[lua] local function scoreIncrease( event)

 if ( event.target:getLabel() == “-” ) then
    print(“pressed = P1-”)
    if ( scoreP1 > 0 ) then
      scoreP1 = scoreP1 - 1

    end [/lua]   

Thank you Bektur, you have saved me!

I also now have a much better understanding of what is going on.

In the end I assigned each widget an ‘id’ (as they had shared label values), and then completed the fucntion using:

if ( event.target.id == “minusP1” ) then

etc…

I very much appreciate your help with this, I will hopefully resume making fast progress now.

All the best,

Alex