【发布时间】: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