Reputation: 21
I wanna create objects forever, after the 6th object was created, i wanna remove the first created one. and then when the 7th was created, i wanna remove the second object. the loops goes like this.
j=0
local tekrarla = function ()
local tekerdusur= {}
j = j+1
print (j)
tekerdusur[j] = display.newSprite( tekeranim, { name="tekergiris2", start=1, count=2, time=800 } )
tekerdusur[j] .x = math.random (display.contentCenterX -400,display.contentCenterX+200)
tekerdusur[j] .y = math.random (display.contentCenterY -300,display.contentCenterY +100)
tekerdusur[j] .bodyType = "dynamic"
tekerdusur[j] .isBullet = true
tekerdusur[j] :play()
physics.addBody( tekerdusur[j] , { density=0.9, friction=0.5, bounce=0.6, radius=38 } )
if (j > 5) then
tekerdusur[j-5]:removeSelf()
tekerdusur [j-5]= nil
end
end
timer.performWithDelay(1000,tekrarla,-1)
thank you.
Upvotes: 0
Views: 99
Reputation: 28991
Create a list for previous sprites. Add new ones to the end. If the list is has 5 entries, remove the oldest (the first) before adding another.
local tekerdusur = {}
local function tekrarla()
local new = display.newSprite( tekeranim, { name="tekergiris2", start=1, count=2, time=800 } )
new.x = math.random (display.contentCenterX - 400, display.contentCenterX + 200)
new.y = math.random (display.contentCenterY - 300, display.contentCenterY + 100)
new.bodyType = "dynamic"
new.isBullet = true
new:play()
physics.addBody( new, { density=0.9, friction=0.5, bounce=0.6, radius=38 } )
if #tekerdusur == 5 then
tekerdusur[1]:removeSelf()
table.remove(tekerdusur, 1)
end
table.insert(tekerdusur, new)
end
timer.performWithDelay(1000, tekrarla, -1)
Upvotes: 2