How to make it so score resets (0) when ball hits ground?

I have entered this for interactivity with the ball –

–Add Event Listener and allow for interactivity
function moveBall(event)
local vx = (ball.x-event.x)/100
local vy=-5.0
ball:applyLinearImpulse( vx, vy, event.x, event.y )
score = score + 1
scoreText.text = score
end

ball:addEventListener(“touch”, moveBall)

The score count goes up once every time I tap the ball, but when it hits the ground, how do I make it so the score automatically goes back to zero? [import]uid: 39628 topic_id: 7558 reply_id: 307558[/import]

You probably need a Runtime:addEventListener to listen for collisions with the ground and use an if statement inside another function to test for event.other then if it’s equal (==) to your variable ground then set the score to zero again. Your’ll probably need to set a global score up too. [import]uid: 33866 topic_id: 7558 reply_id: 26882[/import]

can you type in a code of what that’d look like? i tried a few but im still new to this [import]uid: 39628 topic_id: 7558 reply_id: 26976[/import]

Heres what your main.lua file should roughly look like, if you pop in two graphics for your ball and the ground then the following should give you a good starting point for creating what you were looking for:

[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8) – set up the physics stuff

score = 10 – a global score up, i’ve set it to 10 to test

local ball – ball
local ground – the ground
local scoreText – the score

scoreText = display.newText(score, 100, 10, Helvetica, 50) – sets up the basic score

ball = display.newImage( “ball.png”, 50, 0 ) – ball image
physics.addBody(ball, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.2}) – add physics to the ball so it moves

ground = display.newImage( “ground.png”, 0, 300 ) – ground image
physics.addBody(ground, “static”, {density = 1.0, friction = 0.3, bounce = 0.2}) – set the ground to static but have some bounce and friction

ball.name = “ball” – create a name for the ball object
ground.name = “ground” – create a name for the ground so we can compare
function moveBall(event) – your original function
local vx = (ball.x-event.x)/100
local vy=-5.0
ball:applyLinearImpulse( vx, vy, event.x, event.y )
score = score + 1
scoreText.text = score
end

ball:addEventListener(“touch”, moveBall)
function onCollision( self, event ) – function to test for hitting the ground

if self.name == “ground” and event.other.name ==“ball” then – if the ball hits the ground then
score = 0 – score = 0
scoreText.text = score – set the score text
end
end
ground.collision = onCollision – call function onCollision
ground:addEventListener( “collision”, ground) – add event listener to the ground[/lua] [import]uid: 33866 topic_id: 7558 reply_id: 26993[/import]

it worked ! thank you! any idea what a realistic friction is? [import]uid: 39628 topic_id: 7558 reply_id: 27020[/import]

Hi,

thanks for the great tutorial. i need the code to add a score when the ball falls down to ground , and once a collision is made to remove the object from the screen.

I was playing with the code to add multiple dropped items with a timer, but im unable to get scoring
when using multiple items.

here is the modified code

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8) – set up the physics stuff

score = 100 – a global score up, i’ve set it to 100 to test

local ground – the ground
local scoreText – the score

scoreText = display.newText(score, 100, 10, Helvetica, 50) – sets up the basic score

ground = display.newImage( “assets/graphics/ground.png”, 0, 400 ) – ground image
physics.addBody(ground, “static”, {density = 1.0, friction = 0.3, bounce = 0.2}) – set the ground to static but have some bounce and friction

function dropBalls()

rand = math.random(100)

if (rand < 65) then
j = display.newImage(“assets/graphics/ball.png”);
j.x = 60 + math.random( 45, 160 )
j.y = -100
physics.addBody( j, { density=0.9, friction=0.3, bounce=0.2} )

elseif (rand < 80) then
j = display.newImage(“assets/graphics/ball2.png”)
j.x = 60 + math.random( 45, 160 )
j.y = -100
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.5} )
end
end

– this controls amount of crates drop and the time between (in Milliseconds. 1000 Milliseconds = 1 Sec
local dropBalls = timer.performWithDelay( 1500, dropBalls, 22 )

dropBalls.name = “dropBalls()” – create a name for the ball object
ground.name = “ground” – create a name for the ground so we can compare

function moveBall(event) – your original function
local vx = (dropBalls().x-event.x)/100
local vy=-5.0
dropBalls():applyLinearImpulse( vx, vy, event.x, event.y )
score = score + 10
scoreText.text = score
end

dropBalls():addEventListener(“touch”, moveBall)

function onCollision( self, event ) – function to test for hitting the ground

if self.name == “ground” and event.other.name ==“dropBalls()” then – if the ball hits the ground then
score = score - 5 – score - 5
scoreText.text = score – set the score text
end
end
ground.collision = onCollision – call function onCollision
ground:addEventListener( “collision”, ground) – add event listener to the ground
[/code] [import]uid: 17701 topic_id: 7558 reply_id: 40015[/import]