Hi kind folks, I’m in the process of creating a fun (maybe) game about mazes, but I have problem with clearing array of physical objects, could you help me out a couple of lines of code? I would be very grateful to your if you suggestion/solution helps
@Ignatz i make it like dave1707’s example of multiple objects, so i attach his example .
And this is my main class . I tried call body:destroy() func in line 53 , but this attempt didn’t work
@lupino8211 To get rid of physics objects you don’t want anymore, just use
body:destroy() . If they’re also in a table, remove that entry after the destroy.
@lupino8211 Here’s an example. I create 200 physics objects in a table, destroy them and remove them from the table when they get to the line. The number below the line shows how many objects are in the table.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
tab={}
for z=1,200 do
b=physics.body(CIRCLE,20)
b.x=math.random(WIDTH)
b.y=math.random(HEIGHT,5*HEIGHT)
b.gravityScale=.1
table.insert(tab,b)
end
end
function draw()
background(40,40,50)
fill(255)
text(#tab,WIDTH/2,250)
stroke(255)
strokeWidth(3)
line(0,280,WIDTH,280)
for a,b in pairs(tab) do -- loop to show and check objects
ellipse(b.x,b.y,40)
if b.y<300 then
b:destroy() -- destroy object
b.info="rem" -- set to indicate it needs to be removed
end
end
for z=#tab,1,-1 do -- loop to remove them from the table
if tab[z].info=="rem" then
table.remove(tab,z) -- remove from table
end
end
end
@lupino8211 A lot of what I showed will be used even if you don’t use physics objects. So don’t give up on physics just because you don’t understand it. Physics objects are used in a lot of games and make the games easier to code then if you tried to do it without physics objects.
@lupino In that for loop, a is the index of the variable, and b is the variable itself. So since the variable is your physics body, you should call b:destroy(). a is just the information of where the variable is in the table.