In the game I’m making, I 'm trying to reset the score to 0 once the crate collides with the sides. Could anyone please tell me why it isn’t working? The score will not reset.
local function handleCollisionOnDelay( event )
local obj = event.source.object
obj.isBodyActive = false
obj.x = display.contentWidth/2
obj.y = display.contentHeight/2
obj.rotation = 0
timer.cancel( scoreTimer )
currentScore = 0
end
–Function to handle collision
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
--Initiate a very short timer to deactivate the object
local dr = timer.performWithDelay( 10, handleCollisionOnDelay )
--Assign the object reference as a property of the timer
dr.object = self
end
end
crate.collision = onLocalCollision
sky:addEventListener( “collision”, crate )
ground:addEventListener( “collision”, crate )
rightWall:addEventListener( “collision”, crate )
leftWall:addEventListener( “collision”, crate )
– Variables
local currentScore= 0
local scoreTxt = display.newText( "Score: "…currentScore , 100, 200, native.systemFont, 16 )
scoreTxt:setFillColor( 1, 0, 0 )
– Listener for your timer, updates score variable and updates the text
local function scoreKeeper( event )
currentScore = currentScore + 1
scoreTxt.text = "Score: "…currentScore
end
scoreTimer = timer.performWithDelay( 2000, scoreKeeper, -1 )
function crate:touch( event )
if event.phase == “began” then
crate.isBodyActive = true
return true
end
end
crate:addEventListener( “touch”, crate)