I have a display group (spawnedGroup)that I remove upon a certain function (cleanUp) firing. But once this group is gone, other functions that try and insert items into this group cause errors because they don’t have anything to insert into. Here’s my code:
local spawnedGroup = display.newGroup() local transitions = {} local timers = {} local function scooterSpawn (event)timer.performWithDelay(math.random(20,35),scooterSpawn) local scooter = spriteFactory:newSpriteGroup('Scooter 1 moving right') scooter:addPhysics(physics,'dynamic',{}) scooter.x = -100 scooter.y = math.random(120,924) physics.addBody(scooter,"dynamic",{isSensor = true}) scooter.isSensor = true transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end}) spawnedGroup:insert(scooter) end local cleanUp = function() --cancel and nil all the transitions for i=1,#transitions do if transitions[i] then transition.cancel(transitions[i]) transitions[i] = nil end end --cancel and nil all the timers for i=1,#timers do if timers[i] then timer.cancel(timers[i]) timers[i] = nil end end display.remove(spawnedGroup)end local moveCamera = function() transitions[#transition + 1] = transition.to(spawnedGroup, {time = 1000, y = spawnedGroup.y + 1024, onComplete = cleanUp}) end timers[#timers + 1] = timer.performWithDelay(0,scooterSpawn,1)[/code]It's line 14 that's causing the errors. Any possible solutions? Much thanks,Steven [import]uid: 79394 topic_id: 15389 reply_id: 315389[/import]
you cannot insert objects to a group which is removed. don;t remove the group if you will add more objects to it later. you may instead try to delete all objects from inside the group.
you may use
[lua]while spawnedGroup.numChildren > 0 do
– delete the items from last to first
spawnedGroup[spawnedGroup.numChildren]:removeSelf()
end[/lua]
instead of display.remove(spawnedGroup) inside the function cleanUp
see the example below
[lua]local group = display.newGroup()
local item1 = display.newRect(50,50,50,50)
group:insert(item1)
local item2 = display.newRect(150,150,50,50)
group:insert(item2)
print(group.numChildren)
while group.numChildren > 0 do
– delete the items from last to first
group[group.numChildren]:removeSelf()
end
print(group.numChildren)[/lua] [import]uid: 71210 topic_id: 15389 reply_id: 56878[/import]
Hi Renjith,
Thanks for the response. Will this method be more processor intensive than just removing it? I wasn’t planning on adding more objects to the group later, just making new groups. What do you think would be the best practice?
Steven [import]uid: 79394 topic_id: 15389 reply_id: 57029[/import]
even removing of group will be doing the same thing. But of course there will be performance improvement if you remove the group as such. if you don’t add any objects later into that group then how will you got the error you mentioned ? [import]uid: 71210 topic_id: 15389 reply_id: 57032[/import]
I’m not sure if I’m understanding the working of the code exactly, but in my mind, the group is getting removed by the cleanUp function, but the line I mentioned is still trying to insert things into the group that doesn’t exist anymore, causing the error. Am I wrong? I’m probably confused…
[import]uid: 79394 topic_id: 15389 reply_id: 57033[/import]
Hi Renjith,
Either way, your code fix worked like a charm. I think I’ll take it as is, as your method doesn’t seem to impede performance in the least:)
Thank you once again!
Steven
PS your code example was verry helpful, cheers:) [import]uid: 79394 topic_id: 15389 reply_id: 57034[/import]
if you can send the code with the error to my email i will check it.
[import]uid: 71210 topic_id: 15389 reply_id: 57035[/import]