localGroup:insert() : informations

Hi everybody,

I try to use the great Ricardo class, but I must admit I don’t understand the :insert() thing.

Assuming I have sample code (a former main.lua) that I want to put in a scene (exactly like the demo does with screen1 and screen2.lua) :

Mainly it’s said that I have to put my code here :

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 ------ Your code here ------  
  
 -- MUST return a display.newGroup()  
 return localGroup  
end  

Okay. I guess that any display object have to endure the localGroup:insert(displayObject), but what about variables or arrays, or functions ?

If not inserted in the localGroup, they won’t be cleaned by the director class clean up method automatically called on each scene change, is that correct ?

Shortly : what should be localGroup:insert() ?

Thx for any hints. [import]uid: 9328 topic_id: 6081 reply_id: 306081[/import]

I realize I’m not that clear.
I see ricardo video saying insert should be done for all display objects.
But I’m still worried about eventlisteners : director class auto cleans every listeners when changing scenes ? [import]uid: 9328 topic_id: 6081 reply_id: 21015[/import]

@Antheor

The localGroup is needed to clean display objects from the screen. After that, Director try to unload the file from execution.

Let’s talk about the modules.

When you want to use an outside module to store functions and variables, you have to use this command:

local myPackage = require("myPackage")  

While you are executing your program, your package will stay at the memory. What Director does is just try to remove it from the memory with all it’s functions and variables.

The only things that Director can’t remove are timers and Runtime listeners. [import]uid: 8556 topic_id: 6081 reply_id: 21138[/import]

Thx for your answer.
I mus admit the use of modules to store functions and variables is obscure to me (I’ll dig into it later).

Concerning listeners, I suppose you’re talking only about runtime listeners.

In your code example screen 2 :

 -- Touch to go back  
 local function touched ( event )  
 if event.phase == "ended" then  
 director:changeScene("screen1","fade")  
 end  
 end  
 background:addEventListener("touch",touched)  

You don’t have to add something like

background:removeEventListener("touch",touched)
before the changeScene, don’t you ? [import]uid: 9328 topic_id: 6081 reply_id: 21148[/import]

@Antheor

Yes, I was talking about the Runtime Listeners only. Director goes through every object inserted into the localGroup and calls the removeSelf function. This function removes the image and the listeners.

As the Runtime listeners aren’t associated to a display object, Director can’t find them to remove. [import]uid: 8556 topic_id: 6081 reply_id: 21391[/import]

One more thing : if I add, at the beginning, a lib like :

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 ------ Your code here ------  
 local Particles = require("lib\_particle\_candy")  

This works fine !

module(..., package.seeall)  
local Particles = require("lib\_particle\_candy")  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 ------ Your code here ------  
  

This works fine too !

So what is the best practice ? In the second code the “require” is not inside function new() : that could bring some issues ?

Thx
[import]uid: 9328 topic_id: 6081 reply_id: 21548[/import]

@Antheor

I always put my require functions at the beginning so I can use them wherever I want. [import]uid: 8556 topic_id: 6081 reply_id: 21844[/import]