2014-09-20 48 views
1

我正在为Corona SDK开发一款简单的球类型游戏,而且我现在希望游戏的得分在触摸屏幕上的球时增加1分。目前,每当它发生时,分数的文本变量就消失了,没有其他事情发生。我如何让分数增加? 这里是我的代码:如何使触摸分数增加得分?


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 
+0

在您的代码行3,“本地分数= 0;”本地表示变量“分数”仅用于此函数,但函数“touchBall(event)”将被多次调用,当调用此函数时,变量“score”将被设置为0,因此您应该将“local score = 0;“脱离“touchBall(event)”功能。 – Albert 2014-09-22 01:30:19

+0

实际工作,谢谢! – 2014-09-23 00:13:17

回答

1

创建运行时监听器以保持比分的变化。

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

删除第15行并插入它,如上所述。

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