我希望只有在按下此按钮时分数才会增加。但是,当我尝试运行我的代码时,分数没有改变......任何想法? 提前谢谢!
这是我的代码:
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)
答案 0 :(得分:1)
首先你声明:
local buttonPressed = false
结束然后再将其声明为函数:
local function buttonPressed(event)
重命名你的功能,一切都会顺利