It appears that what I'm about to post has posed a problem to many (for example, a variation of it appears here:http://developer.anscamobile.com/forum/2010/09/06/problem-removeself-when-adding-bodys-table). Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | --import the ; class for sharper text local retina = require("retina"); --Create table to hold missiles local silo = {} --Index for the silo table local ndx = 0; --spawn missiles on command function spawnMissiles() ndx = ndx + 1; --Create a single instance of a missile (to be assigned to multiple table entries later --This is a custom method that loads newImageRect's) silo[ndx] = retina.newImage({image="images/missile.png",width=70,height=36,origin="center-right"}); silo[ndx].x = -80; silo[ndx].y = display.contentHeight/2; transition.to(silo[ndx], {time=1500, x=500, onComplete=removeMissiles}); end --remove missiles if they cross the left-half of the screen function removeMissiles() for i = 1, #silo do local s = silo[i] if (s.x) then print(s.x) if (s.x > _W/2) then s:removeSelf(); end end end end --spawn 10 missiles spwnr = timer.performWithDelay(500, spawnMissiles, 10); |
My bad: I mean to replace the _W constant with display.contentWidth -- here's the correct faulty code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | --import the ; class for sharper text local retina = require("retina"); --Create table to hold missiles local silo = {} --Index for the silo table local ndx = 0; --spawn missiles on command function spawnMissiles() ndx = ndx + 1; --Create a single instance of a missile (to be assigned to multiple table entries later --This is a custom method that loads newImageRect's) silo[ndx] = retina.newImage({image="images/missile.png",width=70,height=36,origin="center-right"}); silo[ndx].x = -80; silo[ndx].y = display.contentHeight/2; transition.to(silo[ndx], {time=1500, x=500, onComplete=removeMissiles()}); end --remove missiles if they cross the left-half of the screen function removeMissiles(obj) for i = 1, #silo do local s = silo[i] if (s.x) then print(s.x) if (s.x > display.contentHeight/2) then s:removeSelf(); end end end end --spawn 10 missiles spwnr = timer.performWithDelay(500, spawnMissiles, 10); |
oh!
I got the same error too !
removeSelf() seems only work on display items,fail with table objects.
but how can I clean up memory ?
anyone got ideas ?
thanks.
removeSelf is a function added by Corona to display objects tables. This is not a standard lua function. To delete a regular table, just nil it and it will be GCed.
views:1305 update:2011/10/2 9:44:12