【问题标题】:how to make a button pressed only once in corona sdk and not repeatedly?如何使一个按钮在电晕 sdk 中只按下一次而不是重复?
【发布时间】:2014-07-15 10:30:06
【问题描述】:

我正在制作一个问题应用程序,当您选择正确答案按钮时,分数会添加 +1,选择错误答案时会添加 -1, 我怎样才能使按钮能够被按下一次,但之后就不能再按下?因为如果你按住按钮,分数会不断增加!

这是按钮 1:

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
}

这是分数函数..也许有一个分数加1然后停止的方法:

--------分数------------------------

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)

【问题讨论】:

    标签: sdk lua coronasdk


    【解决方案1】:

    你可以这样做:

    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
    

    【讨论】:

    • 您可能还想将按钮的 alpha 更改为 .5 以显示在同一功能中已禁用。
    • 成功了!非常感谢!我一直在努力寻找这个问题的答案!
    • 有没有办法在移动到下一个场景后阻止按钮被按下?
    • 是的,看看这个question。在您的情况下, minusButtonPressed 变量是您要保存其状态的变量。有很多方法可以做到这一点,它们各有优缺点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-05-26
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多