I’m quite confused, and I don’t know why the Corona online example removes objects from table this way:
(http://docs.coronalabs.com/api/type/DisplayObject/removeSelf.html)
– Manually cull offscreen objects
- local function removeOffscreenItems()
- for i = 1, #allItems do
- local anItem = allItems[i]
- if (anItem.x) then
- if anItem.x < -100 or anItem.x > display.contentWidth + 100
- or anItem.y < -100 or anItem.y > display.contentHeight + 100 then
- anItem:removeSelf()
- end
- end
- end
It doesn’t set anItem to nil. Does not loop through table in reverse, and it doesn’t actually remove the objects from the table, correct? I tried it and it doesn’t seem to work.
I’ve got the bottom two code segments to work, but wanted to make sure with the pros, if I was doing it right, or what the proper way is:
- for i=#self.allStuff,1, -1 do
- if self.allStuff[i] then
- if (self.allStuff[i].y > screen.bottom) or
- (self.allStuff[i].y < screen.top) or
- (self.allStuff[i].x < screen.left) or
- (self.allStuff[i].x > screen.right) then
- oneStuff = self.allStuff[i]
- display.remove (self.allStuff[i]) – is this line better
- --oneStuff:removeSelf() – or is this line better?
- --self.allStuff[i]:removeSelf() – why is this line wrong?
- table.remove (self.allStuff,i)
- oneStuff = nil
- end
- end
- end
- ---- or --------- ?
- for i=#self.allStuff,1, -1 do
- if self.allStuff[i] then
- if (self.allStuff[i].y > screen.bottom) or
- (self.allStuff[i].y < screen.top) or
- (self.allStuff[i].x < screen.left) or
- (self.allStuff[i].x > screen.right) then
- local oneStuff = table.remove (self.allStuff,i)
- if oneStuff ~=nil then
- oneStuff:removeSelf()
- oneStuff=nil
- end
- end
- end
- end