Reputation: 11
I'm working on a simple ball-type game for Corona SDK, and I currently want the game's score to increase by one whenever a ball on screen is touched. Currently whenever it happens the text variable for score disappears and nothing else happens. How do I make the score increase? Here's my code:
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
Upvotes: 1
Views: 329
Reputation: 1949
Create a Runtime Listener to maintain the score changes.
local function runtimeListener( event )
scoreNum.text=score
end
Runtime:addEventListener("enterFrame",runtimeListener)
Remove the line 15 and insert it as given above.
This makes the score keep on changing according to the touches..
Upvotes: 1