【问题标题】:How to make score increase score on touch?如何使得分增加得分?
【发布时间】:2014-11-15 02:55:13
【问题描述】:

我正在为 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)



end 

【问题讨论】:

  • 在您的代码第 3 行中,“local score = 0;” local表示变量“score”只用于这个函数,但是函数“touchBall(event)”会被多次调用,调用这个函数时变量“score”会被设置为0,所以你应该把“local score” = 0;"超出函数“touchBall(event)”。
  • 确实有效,谢谢!

标签: android mobile lua coronasdk


【解决方案1】:

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

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

删除第 15 行并按照上面给出的方式插入。

这使得分数根据触摸不断变化..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2014-02-09
    相关资源
    最近更新 更多