[英]how to make a button pressed only once in corona sdk and not repeatedly?
我正在制作一个问题应用程序,当您选择正确的答案按钮时,分数会添加+1,选择错误答案时会添加-1,如何使按钮能够被按下一次,但之后又无法再次按下? 如果您持续按下按钮,分数会不断增加!
这是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
}
这是得分功能。.也许有一种方法是得分加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)
您可以执行以下操作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.