0
votes

Hi guys can anyone help me on how I would code a timer update which increases on colliding with a specific object using corona sdk and LUA.

Basically a timer will countdown and if a player collides with a star then the timer should go + 5 etc.

heres my timer set up

function timerDown()
  timeLimit = timeLimit-1
  timeLeft.text = timeLimit

And the star

star = display.newImage("star1.png")
  star.name = "star"
  star.x = 700
  star.y = 200
  physics.addBody(star, "static")

Thanks guys.

1
From Corona labs API documentation on physics.addBody() NOTE: This API should not be used in a Collision Event handler. - hjpotter92
What is the right way it should be done? im new to corona and LUA. - Dips

1 Answers

0
votes
local timeLimit

function timerDown()
  timeLimit = timeLimit-1
  timeLeft.text = timeLimit
end

function newFunc( event )
  if event.phase == "began" then
     timeLimit = timeLimit + 5
  end
end

star = display.newImage("star1.png")
  star.name = "star"
  star.x = 700
  star.y = 200
  physics.addBody(star, "static")
  star:addEventListener( "collision", newFunc )

timer.performWithDelay( 1000, function() timerDown() end, timeLimit )

This will help you to start. Depending on your objects, you may need some modifications on collision listener.