Hesa
Hesa

Reputation: 7

how to make a button pressed only once in corona sdk and not repeatedly?

im making a questions app that when you pick the correct answer button a +1 is added to the score and -1 when selecting wrong answer, How can I make buttons able to be pressed once, but then not pressable again after that? since score keeps adding if you keep pressing on the button!

this is button1:

local widget = require( "widget" )


local function handleButtonEvent( event )

    if ( "ended" == event.phase ) then 
    minusScore()

        print( "Button was pressed and released" )
    end
end 

local button1 = widget.newButton
{
    width = 350,
    height = 360,
    left= 30,
    top= 220,
    defaultFile = "speakers.png",
    overFile = "wrong.png",
    --label = "button",
    onEvent = handleButtonEvent
}

this is the score function..maybe theres a way that the score adds 1 then stops:

-------------------score------------------------

local score = 0
local scoreTxt = display.newText( "0", 0, 0, "Helvetica", 40 ) 
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 700
scoreTxt.y = display.screenOriginY + 37
scoreTxt:setTextColor(2,2,2)
---------------------score added 10-----------------------------
function updateScore()
    score = score + 1
    _G.score = score
    scoreTxt.text = string.format(" %d", score)
end
local scoretimer = timer.performWithDelay(1, updateScore,1)
---------------------score minus 1-----------------------------
 function minusScore()
    score = score - 1
    _G.score = score
   scoreTxt.text = string.format(" %d", score)
end
local scoretimer = timer.performWithDelay(1, minusScore,1)

Upvotes: 0

Views: 1097

Answers (1)

ryosua
ryosua

Reputation: 65

You could do something like this:

local minusButtonPressed = false

local function handleButtonEvent( event )
    if ( ( "ended" == event.phase ) and (minusButtonPressed == false) )  then 
        minusScore()
        print( "Button was pressed and released" )
        --disable the button
        minusButtonPressed = true
    end
 end

Upvotes: 1

Related Questions