Hello, This is a sample project i made to help you understand how to do what you want…
NOTE: GO TO CORONA SIMULATOR AND MAKE A NEW PROJECT AND OPEN MAIN.LUA THEN ADD THIS CODE INTO IT AND SAVE AND DRAG THE WHITE BOX IT THE RED BOX AND LET GO AND LOOK AT THE COSOLE AND SEE WHAT HAPPENS… THEN DRAG THE WHITE BOX OUT OF THE RED BOX AND THEN YOULL SEE THAT THE CONSOLE STOPS PRINTING “-1 Health”
local physics = require("physics") physics.start() local healthTimer local lava = display.newRect( display.contentCenterX, 250, 70, 70 ) physics.addBody( lava, "static", { bounce = 0 }) lava:setFillColor( 1, 0, 0 ) local player = display.newRect( display.contentCenterX, 100, 50, 50 ) physics.addBody( player, "dynamic", { bounce = 0 }) player.gravityScale = 0 local function dragPlayer(event) local player = event.target if event.phase == "began" then player.moving = true player.parent:insert(player) display.getCurrentStage():setFocus( player ) player.x0 = event.x - player.x player.y0 = event.y - player.y elseif event.phase == "moved" then if player.moving == false then return false end player.x = event.x - player.x0 player.y = event.y - player.y0 elseif event.phase == "ended" or event.phase == "cancelled" then player.moving = false display.getCurrentStage():setFocus( nil ) end end player:addEventListener( "touch", dragPlayer ) local function duringLava() print("-1 Health") end local function onCollision(event) if event.phase == "began" then healthTimer = timer.performWithDelay( 1000, duringLava, -1) elseif event.phase == "ended" then timer.cancel(healthTimer) end end player:addEventListener( "collision", onCollision )
So this sample code works like this… so when the player is collided with the lava then the healthTimer starts and when the health timer starts it calls the duringLava function one timer every
second infinite times and when the collision ended the timer gets cacelled… So in the duringLava function you could add your health remover function or whatever you might have.
Good Luck!