如何使得触摸增加得分?

我正在为 Corona SDK 开发一个简单的球类游戏,现在我想让游戏的分数在屏幕上的球被触摸时增加一分。每当发生这种情况时,分数的文本变量就会消失,什么都不会发生。我该如何使分数增加呢? 以下是我的代码:

function touchBall(event)
    local ball = event.target
    local score = 0;
    scoreNum.text = score
    scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
    score = score + 1
    ball_h = 5
    
    ball:applyLinearImpulse(0, -0.2, event.x, event.y)
    ball_h = ball.y
    if ball_h > 50 then
        gameover();
    end
    if event.target == "touch" then
        score = score + 1
        scoreNum.text = score
    end
    
end

ball:addEventListener("touch", touchBall)
ball2:addEventListener("touch", touchBall)
ball3:addEventListener("touch", touchBall)
点赞
用户4014369
用户4014369

创建一个运行时监听器以维护分数的变化。

        local function runtimeListener(event)
           scoreNum.text = score
        end
        Runtime:addEventListener("enterFrame",runtimeListener)

删除第15行并插入如上所示的内容。

这样就可以根据触摸使得分数不断变化。

2014-09-23 10:42:44