removing object creted in loop help

Hi,

I have been messing with joints, I used Carlos’ ball on a rope code as a reference.

Now my problem is, how the heck do I destroy the rope ?

I have tried many things, using pairs and ipairs but no joy whatsoever.

I am missing something simple I feel can anybody help and point me in the correct direction please.

[lua]

local physics = require (“physics”)
physics.start ()
physics.setGravity (0, 10)
 
local ceiling   = display.newRect (0, 0, display.contentWidth, 1)
physics.addBody (ceiling, “static”, {bounce = 0.0, friction = 10})

local xCenter = 160
local hCeil = -5

local prevBody = ceiling

local w,h = 10,10
local halfW,halfH = 0.5*w,0.5*h

local x = xCenter
local y = hCeil - halfH
local yJoint = y - halfH

for i = 1, 20 do
    y = y + h
    yJoint = yJoint + h
    
     local chainlinks = display.newImage(“rope.png” ,x-halfW, y-halfH)
     physics.addBody( chainlinks, { density=15, friction=0.5, bounce = .2 })

     local chainjoint = physics.newJoint( “pivot”, prevBody, chainlinks, xCenter, yJoint )
     newBody = chainlinks
    
     function removeLinks()

       print(“removelinks called”) --CODE HERE TO REMOVE THE ROPE–
       
     end    
end

local key = display.newImage(“soccerball.png”, x,y -30)
physics.addBody( key, { density=2, friction=0.5, bounce=.2, radius=20  })

local joint = physics.newJoint( “pivot”, newBody, key, xCenter, y )

local function removeEverything()

   display.remove(ceiling)
   ceiling = nil
   removeLinks()
   display.remove(key)
   key = nil
end

timer.performWithDelay(2000,removeEverything,1)

[/lua]

Store them somewhere :slight_smile:

Haha,

yup, stored in a table and then removed like so:

for i = #chainlinks,1,-1 do
         chainlinks[i]:removeSelf()
         chainlinks [i] = nil
         end

thanks

Keep in mind when you nil an object in the middle of the table, #tablename returns a count upto the nil and you loose access to the rest of them.  There is a table function (table.maxn I think) that gets you the full length of the table.

So you can do:

for i = table.maxn(chainlinks), 1, -1 do

     if chainlinks[i] and chainlinks[i].removeSelf then

           chainlinks[i]:removeSelf()

           chainlinks[i] = nil

     end

end

or you can to a table.remove() on the cell you just removed (though if its a big list that your checking a lot, its a bit pokey to do

@Rob,

great, that takes care of things better.

Thanks for taking time to reply chaps :slight_smile:

Rob, is there any performance disadvantage to using in pairs() instead of table.maxn() when you have gaps?

You probably meant ipairs().

Pairs() is used to iterate through array with keys not indexes. Maxn() can give you wrong size if table has just keys.

I don’t have any statistics to back things up, but we did a blog post that if I can summarize, ipairs is expensive where #table size is not.  I suspect that ipairs() is more expensive than .maxn() but .maxn() is a bit more costly than #array.  But if you have nil’s, you don’t have much choice.

Store them somewhere :slight_smile:

Haha,

yup, stored in a table and then removed like so:

for i = #chainlinks,1,-1 do
         chainlinks[i]:removeSelf()
         chainlinks [i] = nil
         end

thanks

Keep in mind when you nil an object in the middle of the table, #tablename returns a count upto the nil and you loose access to the rest of them.  There is a table function (table.maxn I think) that gets you the full length of the table.

So you can do:

for i = table.maxn(chainlinks), 1, -1 do

     if chainlinks[i] and chainlinks[i].removeSelf then

           chainlinks[i]:removeSelf()

           chainlinks[i] = nil

     end

end

or you can to a table.remove() on the cell you just removed (though if its a big list that your checking a lot, its a bit pokey to do

@Rob,

great, that takes care of things better.

Thanks for taking time to reply chaps :slight_smile:

Rob, is there any performance disadvantage to using in pairs() instead of table.maxn() when you have gaps?

You probably meant ipairs().

Pairs() is used to iterate through array with keys not indexes. Maxn() can give you wrong size if table has just keys.

I don’t have any statistics to back things up, but we did a blog post that if I can summarize, ipairs is expensive where #table size is not.  I suspect that ipairs() is more expensive than .maxn() but .maxn() is a bit more costly than #array.  But if you have nil’s, you don’t have much choice.