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 |