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]