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?

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.

can you type in a code of what that'd look like? i tried a few but im still new to this

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

it worked ! thank you! any idea what a realistic friction is?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
views:1325 update:2011/10/5 21:23:48
corona forums © 2003-2011