If I am creating multiple physics bodies using a class then how can I destroy a body as soon as a new body is created.
I know this is similar to a question I had asked before but I can’t figure out how to destroy the previous body. If I destroy the previous body the new one is also destroyed while being created and codea crashes
Here’s the code
--# Main
function setup()
body = {}
parameter.watch("#body")
end
function draw()
background(255, 255, 255, 255)
if createBody then
b:draw()
end
if #body>1 then
b.bodies.destroy()
end
end
function touched(touch)
tx = touch.x
ty = touch.y
if touch.state == BEGAN then
b = Bodies()
createBody = true
end
end
--# Bodies
Bodies = class()
function Bodies:init()
self.bodies = physics.body(CIRCLE,10)
self.bodies.x = tx
self.bodies.y = ty
self.bodies.linearVelocity = vec2(-100,0)
self.bodies.gravityScale = 0
table.insert(body,self.bodies)
end
function Bodies:draw()
pushStyle()
fill(255, 0, 0, 255)
ellipse(self.bodies.x,self.bodies.y,20)
popStyle()
end