Ball spawn

I am creating a game and the ball has to spawn down but when I put the ball in my create scene it does nothing but stay there , so I had to put it in my spawn balls function . Now when I try to click the ball to make it disappear this is the error I get . 

attempt to index global 'Ball' (a nil value)

I want the ball to disappear when I click on it 

Can you share the related code?

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 Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.id = "Ball" -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default Ball.x = math.random(10, 300); Ball.y = 100; 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 } ); end end Ball.id = "Ball"

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]

I’m getting the same error

Can you share the related code?

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 Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.id = "Ball" -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default Ball.x = math.random(10, 300); Ball.y = 100; 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 } ); end end Ball.id = "Ball"

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]

I’m getting the same error