First of all, you are using the same variable for every “Ball” object out there. In this case, these are all but one object. Here is what I did:
(Please read the comments to see why I add or remove the line. Also, I may have changed some numbers so feel free to adjust them to your needs)
[lua]
local physics = require “physics”
physics.start( )
local numberBall = 3 --local variable; amount can be changed
local function clearBall( thisBall )
display.remove( thisBall )
thisBall = nil
end
local function spawnBall()
for i=1, numberBall do
local Ball = display.newCircle(100, 100, 50) – when you create it with ‘local’, it won’t be the same Ball object but a new one
Ball:setFillColor( 1, 0, 0 )
physics.addBody(Ball, “dynamic”, {density=0.1, bounce=0.1, friction=0.1, radius=0})
Ball:applyLinearImpulse(-.05, .05, 0, 0) – you first need to add a physics body before you apply linear impulse
Ball.id = “Ball”
Ball.x = math.random(10, 300);
Ball.y = 350;
transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } );
--physics.addBody( Ball, “dynamic”, { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); – redundant because you have already added a physics body to Ball
end
end
spawnBall()
[/lua]