I have this code in setup()
:
print(#physics.bodies)
testbody = physics.body(CIRCLE, 90)
print(#physics.bodies)
testbody:destroy()
testbody = nil
print(#physics.bodies)
According to the documentation, this should print out:
0
1
0
Actual print out is in attached screenshot.
Project also attached.
You need to give it a draw cycle to destroy the body. Other code needs to execute. That goes for just about anything.
@UberGoober Try this code. Tap the screen to create and destroy the body.
function setup()
c=0
fill(255)
str="no physics bodies exist"
end
function draw()
background(40, 40, 50)
text("tap screen",WIDTH/2,HEIGHT/2+100)
text("physics bodies "..#physics.bodies,WIDTH/2,HEIGHT/2)
text(str,WIDTH/2,HEIGHT/2-100)
end
function touched(t)
if t.state==BEGAN then
c=c+1
if c==1 then
testbody = physics.body(CIRCLE, 90)
str="physics body created"
elseif c==2 then
testbody:destroy()
str="physics body destroyed"
elseif c==3 then
testbody = nil
end
end
end
@dave1707 OMG thank you so much. This is a vital thing to know and I’ve honestly been banging my head against it for a looooong time.
I slightly adjusted the code so that it repeatedly creates and destroys bodies:
function setup()
c=0
fill(255)
str="no physics bodies exist"
end
function draw()
background(40, 40, 50)
text("tap screen",WIDTH/2,HEIGHT/2+100)
text("physics bodies "..#physics.bodies,WIDTH/2,HEIGHT/2)
text(str,WIDTH/2,HEIGHT/2-100)
end
function touched(t)
if t.state==BEGAN then
c=c+1
if c % 2 == 1 then
testbody = physics.body(CIRCLE, 90)
str="physics body created"
elseif c % 2 == 0 then
testbody:destroy()
str="physics body destroyed"
end
end
end
@UberGoober Some things don’t happen instantly. You have to let Codea go thru a code cycle so it can execute any background code that needs to be done.