Problem subtracting points with a counter

0

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

    
asked by Polu 28.10.2016 в 22:41
source

2 answers

2

I do not know the language itself but I realize that you never modify your variable score you only subtract and sample but you never modify it so it will always subtract 500.

score = score - clockText.text
initialtime.text = score
    
answered by 29.10.2016 в 05:05
0

In the end I solved it using json, inserting the punctuation in pointData [1] and recovering it later:

------------------------------SALVAR PUNTOS EN puntos.json--

   local puntoData = {}
		 puntoData[1] = initialtime.text - clockText.text
		 loadsave.saveTable(puntoData,"puntos.json", system.DocumentsDirectory)
         
------------RESCATAR PUNTOS DE puntos.json Y MOSTRARLOS EN initialtime.text------
  
	     puntoData = loadsave.loadTable("puntos.json", system.DocumentsDirectory)
         
local function puntuacion(event)                                            
                   initialtime.text = puntoData[1]
               end 
                   tiempoPuntos = timer.performWithDelay( 100, puntuacion )
    
answered by 31.10.2016 в 11:34