Reset score to 0 upon collision HELP ME!

Hi, I’ve working with corona for about a week.
I made juggling game so every time you tap a ball it receives an impulse causing it to go up.

I have a scoreboard up and running in my game, but i want the score to reset every time the ball touches the floor. I have spent a great amount of time researching how to do this and trying many different functions, but i don’t seem to be doing it right.

[code]local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

score = 0
return true
end
end

ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )[blockcode]

I don’t even know what I’m suppose to do, so i would really appreciate if you guys could help me out.

Thanks! [import]uid: 25129 topic_id: 5558 reply_id: 305558[/import]

Okay i almost figure it out.
The score equals 0 when the ball touches the floor, but it doesn’t reset.

[code]local function onLocalCollision (self, event)
if (event.phase == “began”) then
score = 0
showScore.text = score

end

end

floor.collision = onLocalCollision
floor:addEventListener(“collision”, floor) [import]uid: 25129 topic_id: 5558 reply_id: 18839[/import]

I DID IT! FINALLY!
This is what i did for those who are struggling for a tutorial. Reply to this if anyone has any questions.

[code]local function onLocalCollision (self, event)
if (event.phase == “began”) then
currentScore = 0
showScore.text = currentScore

end

end

floor.collision = onLocalCollision
floor:addEventListener(“collision”, floor) [import]uid: 25129 topic_id: 5558 reply_id: 18842[/import]

Mmmm there doesn’t seem to be any difference between the last 2 posts except the name of the score variable. They both should work. You probably had something else in your code changing “score” back up from zero. [import]uid: 10835 topic_id: 5558 reply_id: 18946[/import]

I am doing a similar program except my trouble is with the linearImpulse function, do you put the collision function inside or outside the Impulse function? It would be great if you could show me some of your code! [import]uid: 38677 topic_id: 5558 reply_id: 23476[/import]

This is all of my code:

--\> Hide status bar  
display.setStatusBar( display.HiddenStatusBar )   
   
--\> Add physics engine, start up the engine, and apply gravity  
local physics = require("physics")   
local gameUI = require("gameUI")  
physics.start()  
physics.setGravity(0, 100)   
  
system.activate( "multitouch" )  
  
--physics.setDrawMode("hybrid")  
  
  
--\> Add background image  
local background = display.newImage("background2.png")  
  
--\> Add balls to stage and position  
local ball = display.newImage("hacky2.png")  
ball.x = display.contentWidth/2   
physics.addBody(ball, {density = 6 , bounce = .1, radius = 75, friction = 1.0} )  
-- Define wall graphics (rectangles)  
local leftWall = display.newRect(-1, 0, 1, display.contentHeight )  
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight )  
local ceiling = display.newRect(0, 1, display.contentWidth, 1 )  
local floor = display.newRect(0, 970, display.contentWidth, 0 )  
-- Turn wall graphics into physical bodies  
physics.addBody(leftWall, "static", { bounce = 0.2} )   
physics.addBody(rightWall, "static", { bounce = 0.5} )   
physics.addBody(ceiling, "static", { bounce = 0.3} )  
physics.addBody(floor, "static", { bounce = 0, friction = 200} )  
--\> Add floor image and position  
-- Define our touch event listener.  
function doImpulse(event)   
media.playEventSound("click.caf")  
 ball:applyLinearImpulse( -8000, -10000, event.x, event.y )  
end  
  
-- Add the listener to our ball  
ball:addEventListener("touch", doImpulse)  
  
ball.angularDamping = 6  
local d = ball.angularDamping  
  
  
--------Score  
  
io.output():setvbuf('no') -- disable output buffering for console in device!  
  
Json = require("Json")  
score = require("score")  
   
local currentScore =0  
   
local scList=0  
   
   
local bestScore = 0  
--   
-- ////////////////////  
--  
   
local showScore=display.newText( "", 250, 10, nil, 30)  
showScore:setTextColor( 0,0,0 )  
   
local yourScore=display.newText( "YOUR SCORE:", 20, 10, nil, 30)  
yourScore:setTextColor( 0,0,0 )  
   
local showBestScore=display.newText( "", 550, 10, nil, 30)  
showBestScore:setTextColor( 0,0,0 )  
   
local yourBestScore=display.newText( "BEST SCORE:", 320, 10, nil, 30)  
yourBestScore:setTextColor( 0,0,0 )  
  
   
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
function ball:touch(event)  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 currentScore=currentScore+1  
 if currentScore\>=bestScore then scList:StoreEntry(1,currentScore,"best") end  
 scList:Save("score3.txt")  
 bestScore=scList:GetPoints(1)  
 showScore.text=currentScore  
 showBestScore.text=bestScore  
end  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
local onSystemEvent = function( event )   
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 print ("onSystemEvent-\>"..event.type)  
 if event.type == "applicationExit" then   
 --  
 elseif event.type == "applicationStart" then   
 print ("applicationStart")   
 scList = score.newList(1)  
 scList:Load("score3.txt")  
 bestScore=scList:GetPoints(1)  
 print ("bestscore:"..bestScore)   
  
 elseif "applicationSuspend" == event.type then  
 tSuspend = system.getTimer()   
 elseif "applicationResume" == event.type then   
 --add missing time to tPrevious   
 tPrevious = tPrevious + ( system.getTimer() - tSuspend )   
 end   
end  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
local onFrame = function( event )   
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
end  
  
ball:addEventListener("touch", ball)  
Runtime:addEventListener( "system", onSystemEvent )  
Runtime:addEventListener( "enterFrame", onFrame )  
  
--currentScore = 0 Function  
  
local function onLocalCollision (self, event)  
 if (event.phase == "began") then  
 currentScore = 0   
 showScore.text = currentScore  
 end  
  
end  
   
   
floor.collision = onLocalCollision  
floor:addEventListener("collision", floor)  
local start = display.newImage("start.png")  
  
local function disappear ( event )  
 transition.to(start, {time = 500, alpha = 0} )   
end  
   
Runtime:addEventListener("touch", disappear)  

Im not sure but i also think you have to have this file as a separate file in your game folder and name it “gameUI.lua”…my game doesn’t work without it:

-- gameUI library  
module(..., package.seeall)  
  
-- A general function for dragging physics bodies  
  
-- Simple example:  
-- local dragBody = gameUI.dragBody  
-- object:addEventListener( "touch", dragBody )  
  
function dragBody( event, params )  
 local body = event.target  
 local phase = event.phase  
 local stage = display.getCurrentStage()  
  
 if "began" == phase then  
 stage:setFocus( body, event.id )  
 body.isFocus = true  
  
 -- Create a temporary touch joint and store it in the object for later reference  
 if params and params.center then  
 -- drag the body from its center point  
 body.tempJoint = physics.newJoint( "touch", body, body.x, body.y )  
 else  
 -- drag the body from the point where it was touched  
 body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )  
 end  
  
 -- Apply optional joint parameters  
 if params then  
 local maxForce, frequency, dampingRatio  
  
 if params.maxForce then  
 -- Internal default is (1000 \* mass), so set this fairly high if setting manually  
 body.tempJoint.maxForce = params.maxForce  
 end  
  
 if params.frequency then  
 -- This is the response speed of the elastic joint: higher numbers = less lag/bounce  
 body.tempJoint.frequency = params.frequency  
 end  
  
 if params.dampingRatio then  
 -- Possible values: 0 (no damping) to 1.0 (critical damping)  
 body.tempJoint.dampingRatio = params.dampingRatio  
 end  
 end  
  
 elseif body.isFocus then  
 if "moved" == phase then  
  
 -- Update the joint to track the touch  
 body.tempJoint:setTarget( event.x, event.y )  
  
 elseif "ended" == phase or "cancelled" == phase then  
 stage:setFocus( body, nil )  
 body.isFocus = false  
  
 -- Remove the joint when the touch ends   
 body.tempJoint:removeSelf()  
  
 end  
 end  
  
 -- Stop further propagation of touch event  
 return true  
end  
-- A function for cross-platform event sounds  
  
function newEventSoundXP( params )  
 local isAndroid = "Android" == system.getInfo("platformName")  
  
 if isAndroid and params.android then  
 soundID = media.newEventSound( params.android ) -- return sound file for Android  
 elseif params.ios then  
 soundID = media.newEventSound( params.ios ) -- return sound file for iOS/MacOS  
 end  
  
 return soundID  
end  
-- A function for cross-platform fonts  
  
function newFontXP( params )  
 local isAndroid = "Android" == system.getInfo("platformName")  
  
 if isAndroid and params.android then  
 font = params.android -- return font for Android  
 elseif params.ios then  
 font = params.ios -- return font for iOS/MacOS  
 else  
 font = native.systemFont -- default font (Helvetica on iOS, Android Sans on Android)  
 end  
  
 return font  
end  

Let me know if you have any questions. [import]uid: 25129 topic_id: 5558 reply_id: 23707[/import]