I created this script to subtract points from a game, and it works, but not as I want, I explain:
"initialtime" shows me 500 on the display, at this 500 I want to subtract the elapsed time of the "clockText" counter every time "ball" touches "platform". What I try to achieve is that if the ball touches the platform and 1 second has elapsed, at 500 it would deduct 1 second and there would be 499, but since the counter is still running, if 3 seconds have elapsed, I do not want to subtract 3 seconds from 500, I want it to be subtracted at 499 - 3 = 496, and so on:
---SI---------|-----------NO
|
500 -1-------|---------500 -1
|
499 -3-------|---------499 -3
|
496 -5-------|---------497 -5
|
491----------|---------495
Concluding, the ball would keep bouncing and every time it bounces, it will be subtracted from the result of initial time and not 500.
The code:
local physics = require "physics"
physics.start()
local score = 500
----Initial Time----
local initialtime = display.newText(score, 100, 200, "Helvetica", 40)
-----Accountant-----
local clockText = display.newText("000", 100, 300, "Helvetica", 40)
local secondsLeft = 0 -- secons
local function updateTime()
secondsLeft = secondsLeft + 1
seconds = secondsLeft % 60
timeDisplay = string.format("%03d", seconds )
clockText.text = timeDisplay
end
countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )
-----Collision-----
local platform = display.newRect( 0, 0, 280, 30 )
platform.surfaceType = "superbounce"
platform.x, platform.y = display.contentCenterX, display.contentCenterY+200
physics.addBody( platform, "static", { bounce=0.0, friction=0.3 } )
local ball = display.newCircle( 0, 0, 15 )
ball.x, ball.y = display.contentCenterX+100, display.contentCenterY-40
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )
local function onCollision( self, event )
local collideObject = event.other
if ( collideObject.surfaceType == "superbounce" ) then
event.contact.bounce = 1
print(score - clockText.text)
initialtime.text = score - clockText.text
end
end
ball.collision = onCollision
ball:addEventListener( "collision" )
Ok, Thanks for answering sioesi. Your idea works:
score = score - clockText.text
initialtime.text = score
But I subtract twice as much as clockText. When you boot the first time and clockText dials 1, initialtime will subtract 2 instead of 1:
First pot: 500 -1 = 498 Second pot: 498 -3 = 492 third pot: 492 -5 = 482