myBody:destroy() problems

For some reason, whenever I type ball:destroy() (ball is my physics body) it has an error. Please help

Here is the code:

function setup()
parameter.action("Destroy Ball", function() ball:destroy()end)
   ball = physics.body(CIRCLE,25)
ball.x =WIDTH/2
    ball.y=HEIGHT-100 
    ball.gravityScale = 2
ball.restitution = 0.8
f = physics.body(EDGE, vec2(0,0),vec2(WIDTH,0))
end

function draw()
    background(0, 0, 0, 255)
    
pushStyle()
    fill(255, 0, 0, 255)
ellipseMode(CENTER)
                         ellipse(ball.x, ball.y, 50)
end

You need to read the error carefully

It tells you there is an error in drawing the ellipse

The ellipse command uses ball.x, ball.y and 50 as parameters, so the problem must be with ball.

Oh yes, we just destroyed the ball. So there is no such thing as ball.x any more.

Oops.

You need to fix this by only drawing balls that exist.

See how to debug it?

Oh yes. When I read this I actually laughed at my knowledge of debugging. So, I need to what? If I get rid of ball.x, then I can’t make the ball have physics.

You’ll figure it out

I don’t get it. Please explain it to me.

Wow, I DONT GET IT. If I’m destroying ball, wouldn’t it destroy ball.x and ball.y too? This doesn’t make sense

of course it destroys ball.x

That’s why ellipse gives an error, because you are giving it a nul x position

But ellipse is getting destroyed too. So it doesn’t need an x position

ellipse is not an object

it is a drawing

you can’t destroy an ellipse, because it is a drawing!

so Codea tries to draw it and finds you gave it null x and y, and bugs out

you should STOP drawing the ellipse when the ball is destroyed

How do I do that?

There’s no point trying to make an app if you don’t get how not to draw an ellipse.

I suggest you need to go back to the basics of Lua and Codea.

The forum isn’t the place to explain everything over and over again to new people. That is what the wiki is for, and I have written stacks of blog posts and ebooks to explain this stuff.

Please go and read some more, then see if you get how it all works.

I know how to draw an ellipse, I’m just confused, is there a command that stops drawing?

Trust me, you are more than confused. You really don’t know enough about how Codea works and draws, to make any sort of app.

I’m not going to explain it all again here when I’ve written ebooks about it, which you are welcome to read.

It just makes no sense

I read Codea for beginners and apparently there are tables and stuff

@LL_Phoenix456, here is the fixed version. It will just hide the ellipse, but for you or me who is starting to learn that’s normal I think:

function setup()
    parameter.action("Destroy Ball", function() ball:destroy() ball = {} ball.x = -25 ball.y = -25 end)
    ball = physics.body(CIRCLE,25)
    ball.x =WIDTH/2
    ball.y=HEIGHT-100 
    ball.gravityScale = 2
    ball.restitution = 0.75
    f = physics.body(EDGE, vec2(0,0),vec2(WIDTH,0))
end

function draw()
    background(0, 0, 0, 255)
    pushStyle()
    fill(255, 0, 0, 255)
    ellipseMode(CENTER)
    ellipse(ball.x, ball.y, 50)
end

Thanks, that solved it!

@LL_Phoenix456, you can also replace ball = {} ball.x = -25 ball.y = -25 with
ball = vec2(-25, -25) to save a bit storage.