buttonsdkluacoronasdkpressed

Score activated when button is pressed does not work, Corona sdk


I would like the score to increase only when this button is pressed. However when I tried running my code the score did not change... Any ideas? thanks in advance!

here is my code:

score = 0


local scoreNumber = display.newText(score, 200, 30, nil, 20)
scoreNumber.xScale = 1.2
scoreNumber.yScale = 1.2



local scoreText = display.newText("score:", 150, 30, nil, 20)
scoreText.xScale = 1.2
scoreText.yScale = 1.2



local buttonPressed = false

local myButton = display.newRect(50,50,100,100)

local function scoretimer(event)
if buttonPressed then

    score = score + 1
    scoreNumber.text = score
    end
end

local function buttonPressed(event)
     if event.phase == "began" then
          buttonPressed = true

     elseif event.phase == "ended" then
          buttonPressed = false
     end
     return true
 end

 myButton:addEventListener("touch", buttonPressed)

 Runtime:addEventListener("enterFrame", scoretimer)

Solution

  • First you declare:

    local buttonPressed = false
    

    end then you declare it again as a function:

    local function buttonPressed(event)
    

    Rename your function and everything should go fine