While two objects are in collision reduce health

Can someone please point me in the right direction so that while two objects are detecting a collision their health would keep being reduced. Right now I can only to seem to figure out how to reduce the health once until the collide again. The objects register the collision fine but I need help on figuring out how to flag the objects so that until the objects are no longer colliding the health stops going down.

[code]
local function onCollision( event )

if ( event.phase == “began” ) then
if (event.object1.myName == “Ball”) then
timer.performWithDelay(1000, removeBall(event.object1.myName), 1)

elseif (event.object1.myName == “Gramps” and event.object2.myName == “Ai”) then
PlayerHit = true;

gramps.health = gramps.health - 1;
end
print( "began: " … event.object1.myName … " & " … event.object2.myName )

elseif ( event.phase == “ended” ) then

print( "ended: " … event.object1.myName … " & " … event.object2.myName )
if (PlayerHit) then

end
end
end

[import]uid: 16265 topic_id: 7695 reply_id: 307695[/import]

Start a timer function when the collision begins. Update the score in the function. Cancel the timer when the collision ends [import]uid: 6645 topic_id: 7695 reply_id: 27574[/import]

Thank You will try this out!! [import]uid: 16265 topic_id: 7695 reply_id: 27606[/import]

This worked great!! Had to troubleshoot it some but real quick fixes thanks jmp909

there was a typo in:

P in player hit wasn’t caps that is my bad for using 2 different types of Variable code, some that start with caps and others that don’t still getting my style down.

Also in collisionTimer =

Had to add the “, 0 )” to make it endless.

Thanks a lot appreciate it jmp909

You Rock!

Just posting the fixes encase someone else wants it in the future. [code]
local function reduceScore()
print(“Reduce Score”)
if(PlayerHit) then

collisionTimer = timer.performWithDelay(1500, reduceScore , 0)

[import]uid: 16265 topic_id: 7695 reply_id: 27613[/import]

[lua]local collisionTimer
local PlayerHit=false

local function reduceScore()
if(PlayerHit) then
gramps.health = gramps.health - 1;
end
end

local function onCollision( event )

if ( event.phase == “began” ) then
if (event.object1.myName == “Ball”) then

– fixed this, it would have called immediately
– the way you did it (see note)
timer.performWithDelay(1000,
function ()
removeBall(event.object1.myName)
end
, 1)

elseif (event.object1.myName == “Gramps” and event.object2.myName == “Ai”) then
PlayerHit = true;
– reduce score every second
collisionTimer = timer.performWithDelay(1000, reduceScore,0)
end

elseif ( event.phase == “ended” ) then
if (PlayerHit) then
PlayerHit=false
if(collisionTimer~=nil) then – just to be safe
timer.cancel(collisionTimer)
end
end
end
end[/lua]

note: when you add () on the end of a function, it calls it
immediately. so you cant use this for your timer callback
[lua]timer.performWithDelay(1000,removeBall(event.object1.myName), 1)[/lua]

when you do that, it doesnt wait a second, it calls removeBall straight away, the fix is to wrap it in an anonymous function closure as shown above
[lua]timer.performWithDelay(1000, function () removeBall(event.object1.myName) end, 1)[/lua]
or, the alternative solution is to pass a parameter in the timer object
[lua]function removeBall(event)
local theTimer = event.source
local theParam = theTimer.param
print(theParam)
end

local myTimer = timer.performWithDelay(1000, removeBall)
myTimer.param = event.object1.myName[/lua]

[import]uid: 6645 topic_id: 7695 reply_id: 27582[/import]

My bad sorry , was doing it without testing and thought an empty parameter called the timer infinitely but makes more sense it should only happen once if you don’t put a value [import]uid: 6645 topic_id: 7695 reply_id: 27697[/import]