Something wieird is happening...

Hi Ansca,

I am programming a word game, but with some physics twists. So whenever you find the word bed, it spawns a bed. A cannon also shoots balls out of it. So I wanted the ball to disappear once it collided with the bed. So I put this code in:

[lua]

local function cat()

local flame = display.newImage(“flame.gif”, 422, 68);

transition.to(flame, {time=500, alpha=0});

local ball = display.newImage(“meteor.png”, 430, 65)

ball.yScale = 0.5;

ball.xScale = 0.5;

ball.myName = “ball”;

physics.addBody(ball, “static”, {density=0.4, friction=0.5, bounce=0.1, radius=15});

–ball.collision = coll;

–ball:addEventListener(“collision”, ball);

–lett:insert(ball);

local function fall()

local function falla()

transition.to(ball, {x=ball.x-215, y=ball.y+50, time=900});

end

transition.to(ball, {x=ball.x-50, time=250, onComplete=falla});

end

transition.to(ball, {x=ball.x-215, y=ball.y-50, time=900, onComplete=fall});

–physics.setGravity(-7, -5);

–timer.performWithDelay(650, gchange, 1);

end

 

timer.performWithDelay( math.random(7000, 12000), cat, 0);



local function onLocalCollision( self, event )

        if ( self.myName == “bed” ) and (event.other.myName == “ball”) then

        ball:removeSelf();

        end

end

[/lua]

 

 

 

As you can see, it spawns the ball, and the bed is already spawned. So I did: ball:removeSelf();

And it said in the error section that ball was not defined. Why is it telling me ball is a nil value? I definitely spawned it.

Thanks in advance

Ball is local to the function cat, declare it outside the function, and you should be fine.

I think there is a blog post about scope, search for it, i think you would like it.

Cheers,

Tommy

Or, just reference the ball inside the collision function from the event record.

So instead of ball:removeSelf() you’d do event.other:removeSelf().

Jay

Thanks so much!

event.other:removeSelf() worked! Very excited. Almost done with it!

Thanks so much! event.other:removeSelf() worked! Thx so much for your help. Don’t know what I’d do without the community.

Thanks, it worked!

As you can see, it spawns the ball, and the bed is already spawned. So I did: ball:removeSelf();

And it said in the error section that ball was not defined. Why is it telling me ball is a nil value? I definitely spawned it.

Thanks in advance

Ball is local to the function cat, declare it outside the function, and you should be fine.

I think there is a blog post about scope, search for it, i think you would like it.

Cheers,

Tommy

Or, just reference the ball inside the collision function from the event record.

So instead of ball:removeSelf() you’d do event.other:removeSelf().

Jay

Thanks so much!

event.other:removeSelf() worked! Very excited. Almost done with it!

Thanks so much! event.other:removeSelf() worked! Thx so much for your help. Don’t know what I’d do without the community.

Thanks, it worked!