Problem resetting ball after loosing a life

Hi,

I having problems solving an issue where i try to reset a ball (the player if you will) in my level after loosing a life.

In the collision event that determines the loss condition (losing a life) i use the follwing code to remove the ball:

    [lua]ball:removeSelf();

ball = nil;
resetBall(group);[/lua]

then call a resetBall() function to recreate it in the right location:

[lua]function resetBall(group)
local ball = display.newImage( group, “IMG/ball.png”, 150, -0 )
ball.id = “Ball Object”;
ball.myName = “ball”;
physics.addBody( ball, { density=1.0, friction=0.0, bounce=1, radius=25 } )
ball:applyForce( 500, 100, ball.x, ball.y )
end[/lua]

And i get the error:

attempt to index local ‘ball’ (a nil value) or

attempt to index global ‘ball’ (a nil value) (if i remove local).

I have also tried to add a:  local ball on top, but that gives me the error:

attemt to index upvalue ‘ball’ (a nil value)

Any idea on how to get around this, or a better approach of solving the problem would be very appreciated.

Thanks

I’m newbie, but could you just do a transition where you move the ball back to the proper x and y start location, instead of removing it and recreating it.

My 2¢ hopefully it’s helpful.

Hi thank for you reply,

I tried that moving it, but then i ran into some problems getting the physics to work correctly after the move. 

Anyway i found out my problem was related to not beeing allowed to add physics to the new ball within a collision event. (where i call the resetBall function)

The fix i found was to move the add physics part out into it’s own function and call with it a tiny delay (50ms).

Which line you getting the error at?

Edit: Okay turns out you solved it.

Good to know and thanks for sharing your solution too.

I’m newbie, but could you just do a transition where you move the ball back to the proper x and y start location, instead of removing it and recreating it.

My 2¢ hopefully it’s helpful.

Hi thank for you reply,

I tried that moving it, but then i ran into some problems getting the physics to work correctly after the move. 

Anyway i found out my problem was related to not beeing allowed to add physics to the new ball within a collision event. (where i call the resetBall function)

The fix i found was to move the add physics part out into it’s own function and call with it a tiny delay (50ms).

Which line you getting the error at?

Edit: Okay turns out you solved it.

Good to know and thanks for sharing your solution too.