Spawning and destroying objects into a display group in Storyboard

Hi Everyone,

I have recently started with Lua and i have searched through many web pages about spawning objects but i don’t think i have it quite right.

I have a storyboard setup and into my main group i am putting about 7 other display groups to create layers in my scene.
On one of these layers i want to spawn clouds to float across the sky.
I got this working in a basic example, but now i am trying to do it in a more complex set up it is not working.

local function makeCloud()  
 print ("make cloud called, count " .. cloudInstanceCount)  
 if(cloudsGrp.numChildren) then  
 print ("cloud group num children # " .. cloudsGrp.numChildren)  
 else  
 print ("no children in the group")  
 end  
 local imgpath = "Cloud.png";  
 local imgw = math.random( 50, 200 )  
 local imgh = (imgw / 100 ) \* 59  
 local movementSpeed = 32000;  
 cloudInstanceTable[cloudInstanceCount] = display.newImageRect(imgDir..imgpath, imgw, imgh )  
 cloudInstanceTable[cloudInstanceCount].y = math.random(59,(display.contentHeight/3))  
 cloudInstanceTable[cloudInstanceCount].x = display.contentWidth  
 cloudInstanceTable[cloudInstanceCount].myName = "cloud" .. #cloudInstanceTable + 1  
  
 cloudsGrp:insert(cloudInstanceTable[cloudInstanceCount]);  
  
 cloudInstanceTable[cloudInstanceCount].transition\_id = transition.to(cloudsGrp, {  
 time=movementSpeed,   
 x=-display.contentWidth,   
 onComplete = function(self)cloudsGrp:remove(self); self = nil; end  
 }) -- Move the cloud  
 cloudInstanceCount = cloudInstanceCount + 1  
end -- end makeCloud()  
  
local function startClouds(day)  
 print ("start clouds called")  
 if day == "day" then  
 cloudTimer2 = timer.performWithDelay(32000,makeCloud, 0)  
 end  
end  

I am calling

startClouds(dayNight)  

in the enter scene event and the cloudInstance Table and count are initiated elsewhere in scope (as i get output to the debug prints at the top).
So my problem is that the first cloud floats across the sky, but then the second appears at the end point of the first, then each successive cloud appears in this location, with no transition.

Also it looks like I am not removing these objects properly.
I am sure i am doing something basic wrong and would really appreciate someone more experienced taking a look at this.

Thanks,

Jez [import]uid: 194492 topic_id: 35624 reply_id: 335624[/import]

Jeremy,

Ima newb,

You are moving the Group cloudsGrp in your transition so after your first spawn your next cloud was using the Groups coordinates as a reference I believe.

I can’t tell you why your remove function doesn’t work properly, but I think doing it this way will work for you.

I changed a few references so everything references the “cloudInstanceCount” consistantly.

I also changed the x value of the transition to see the “cloud” being removed before it went off the screen.

I added a modified memory leak function at the bottom, I can’t remember who posted it, it’s been awhile, it may have been Danny. It’s a huge improvement that doesn’t spam your console so you can still read your print statements.

Take a look at this…it’s plug and play!

--Jeremy Spawn and transition remove project  
  
local cloudInstanceTable = {}  
local cloudInstanceCount = 1  
local day = "day"  
  
local cloudsGrp = display.newGroup()  
  
local startClouds  
  
local function makeCloud()  
 print ("make cloud called, count " .. cloudInstanceCount)  
 if(cloudsGrp.numChildren) then  
 print ("cloud group num children # " .. cloudsGrp.numChildren)  
 else  
 print ("no children in the group")  
 end  
 --local imgpath = "Cloud.png";  
  
 local cloud  
  
 local function removeCloud()  
 print("remove(cloud) / cloud == "..cloud)  
 cloudsGrp:remove(cloudInstanceTable[cloud])  
 cloudInstanceTable[cloud] = nil  
 end  
  
  
  
 local imgw = math.random( 50, 200 )  
 local imgh = (imgw / 100 ) \* 59  
 local movementSpeed = 8000;  
 --cloudInstanceTable[cloudInstanceCount] = display.newImageRect(imgDir..imgpath, imgw, imgh )  
 cloudInstanceTable[cloudInstanceCount] = display.newRect(0, 0, imgw, imgh )  
  
 cloudInstanceTable[cloudInstanceCount].y = math.random(59,(display.contentHeight/3))  
 cloudInstanceTable[cloudInstanceCount].x = display.contentWidth  
 cloudInstanceTable[cloudInstanceCount]:setFillColor(255, 255, 255 )  
  
 --print("#cloudsInstanceTable = "..#cloudInstanceTable)  
 print("cloudInstanceCount == "..cloudInstanceCount)  
 cloudInstanceTable[cloudInstanceCount].INDEX = cloudInstanceCount  
 cloudInstanceTable[cloudInstanceCount].myName = "cloud" .. cloudInstanceCount  
 print("cloudInstanceTable[cloudInstanceCount].myName = "..cloudInstanceTable[cloudInstanceCount].myName)  
   
 cloudsGrp:insert(cloudInstanceTable[cloudInstanceCount]);  
   
 cloud = cloudInstanceTable[cloudInstanceCount].INDEX  
   
 cloudInstanceTable[cloudInstanceCount].transition\_id = transition.to(cloudInstanceTable[cloudInstanceCount], {  
 --cloudInstanceTable[cloudInstanceCount].transition\_id = transition.to(cloudsGrp, {  
 time=movementSpeed,   
 --x=-display.contentWidth,  
 x = 0,  
  
 onComplete = removeCloud,  
 }) -- Move the cloud  
 cloudInstanceCount = cloudInstanceCount + 1  
end -- end makeCloud()  
   
function startClouds(day)  
 print ("start clouds called")  
 if day == "day" then  
 cloudTimer2 = timer.performWithDelay(3000,makeCloud, 0)  
 end  
end  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
--This variation only prints changes in the memory status:  
  
local prevTextMem = 0  
local prevMemCount = 0  
  
local monitorMem = function()  
 collectgarbage()  
 local memCount = collectgarbage("count") / 1024  
 if (prevMemCount ~= memCount) then  
 print( "MemUsage: " .. memCount .. "MB")  
 prevMemCount = memCount  
 end  
 local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000  
 if (prevTextMem ~= textMem) then  
 prevTextMem = textMem  
 print( "TexMem: " .. textMem )  
 end  
end  
  
Runtime:addEventListener( "enterFrame", monitorMem )  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
--Memorory Leak Code Above\<\<\<\<\<\<\<\<\<\<\<\<  
startClouds(day)  
  

If you’ve got an iOS device, you should grab my latest app, Super iBot , in the App Store and take a look at it, I sure could use a rating and review.

I hope this helps,

Nail [import]uid: 106779 topic_id: 35624 reply_id: 141645[/import]

Excellent, thank you very much!

I copied this in line by line to see how it was different from my approach and it is all working now.
I put back the cloud images also.

Thank you very much!

I am just installing your app, will definitely leave a review.

Cheers,

Jez [import]uid: 194492 topic_id: 35624 reply_id: 141647[/import]

Cool Jeremy,

I am glad it worked for you and I hope you like Super iBot!

It’s got a lot of neat features in it, if you get stuck, watch the YouTube video cheats.

Thanks for your helping me out!

Nail [import]uid: 106779 topic_id: 35624 reply_id: 141656[/import]

Jeremy,

Ima newb,

You are moving the Group cloudsGrp in your transition so after your first spawn your next cloud was using the Groups coordinates as a reference I believe.

I can’t tell you why your remove function doesn’t work properly, but I think doing it this way will work for you.

I changed a few references so everything references the “cloudInstanceCount” consistantly.

I also changed the x value of the transition to see the “cloud” being removed before it went off the screen.

I added a modified memory leak function at the bottom, I can’t remember who posted it, it’s been awhile, it may have been Danny. It’s a huge improvement that doesn’t spam your console so you can still read your print statements.

Take a look at this…it’s plug and play!

--Jeremy Spawn and transition remove project  
  
local cloudInstanceTable = {}  
local cloudInstanceCount = 1  
local day = "day"  
  
local cloudsGrp = display.newGroup()  
  
local startClouds  
  
local function makeCloud()  
 print ("make cloud called, count " .. cloudInstanceCount)  
 if(cloudsGrp.numChildren) then  
 print ("cloud group num children # " .. cloudsGrp.numChildren)  
 else  
 print ("no children in the group")  
 end  
 --local imgpath = "Cloud.png";  
  
 local cloud  
  
 local function removeCloud()  
 print("remove(cloud) / cloud == "..cloud)  
 cloudsGrp:remove(cloudInstanceTable[cloud])  
 cloudInstanceTable[cloud] = nil  
 end  
  
  
  
 local imgw = math.random( 50, 200 )  
 local imgh = (imgw / 100 ) \* 59  
 local movementSpeed = 8000;  
 --cloudInstanceTable[cloudInstanceCount] = display.newImageRect(imgDir..imgpath, imgw, imgh )  
 cloudInstanceTable[cloudInstanceCount] = display.newRect(0, 0, imgw, imgh )  
  
 cloudInstanceTable[cloudInstanceCount].y = math.random(59,(display.contentHeight/3))  
 cloudInstanceTable[cloudInstanceCount].x = display.contentWidth  
 cloudInstanceTable[cloudInstanceCount]:setFillColor(255, 255, 255 )  
  
 --print("#cloudsInstanceTable = "..#cloudInstanceTable)  
 print("cloudInstanceCount == "..cloudInstanceCount)  
 cloudInstanceTable[cloudInstanceCount].INDEX = cloudInstanceCount  
 cloudInstanceTable[cloudInstanceCount].myName = "cloud" .. cloudInstanceCount  
 print("cloudInstanceTable[cloudInstanceCount].myName = "..cloudInstanceTable[cloudInstanceCount].myName)  
   
 cloudsGrp:insert(cloudInstanceTable[cloudInstanceCount]);  
   
 cloud = cloudInstanceTable[cloudInstanceCount].INDEX  
   
 cloudInstanceTable[cloudInstanceCount].transition\_id = transition.to(cloudInstanceTable[cloudInstanceCount], {  
 --cloudInstanceTable[cloudInstanceCount].transition\_id = transition.to(cloudsGrp, {  
 time=movementSpeed,   
 --x=-display.contentWidth,  
 x = 0,  
  
 onComplete = removeCloud,  
 }) -- Move the cloud  
 cloudInstanceCount = cloudInstanceCount + 1  
end -- end makeCloud()  
   
function startClouds(day)  
 print ("start clouds called")  
 if day == "day" then  
 cloudTimer2 = timer.performWithDelay(3000,makeCloud, 0)  
 end  
end  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
--This variation only prints changes in the memory status:  
  
local prevTextMem = 0  
local prevMemCount = 0  
  
local monitorMem = function()  
 collectgarbage()  
 local memCount = collectgarbage("count") / 1024  
 if (prevMemCount ~= memCount) then  
 print( "MemUsage: " .. memCount .. "MB")  
 prevMemCount = memCount  
 end  
 local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000  
 if (prevTextMem ~= textMem) then  
 prevTextMem = textMem  
 print( "TexMem: " .. textMem )  
 end  
end  
  
Runtime:addEventListener( "enterFrame", monitorMem )  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
--Memorory Leak Code Above\<\<\<\<\<\<\<\<\<\<\<\<  
startClouds(day)  
  

If you’ve got an iOS device, you should grab my latest app, Super iBot , in the App Store and take a look at it, I sure could use a rating and review.

I hope this helps,

Nail [import]uid: 106779 topic_id: 35624 reply_id: 141645[/import]

Excellent, thank you very much!

I copied this in line by line to see how it was different from my approach and it is all working now.
I put back the cloud images also.

Thank you very much!

I am just installing your app, will definitely leave a review.

Cheers,

Jez [import]uid: 194492 topic_id: 35624 reply_id: 141647[/import]

Cool Jeremy,

I am glad it worked for you and I hope you like Super iBot!

It’s got a lot of neat features in it, if you get stuck, watch the YouTube video cheats.

Thanks for your helping me out!

Nail [import]uid: 106779 topic_id: 35624 reply_id: 141656[/import]