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 :

1
2
3
4
5
6
7
8
9
10
11
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

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 ?

@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:

1
local myPackage = require("myPackage")

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 :

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

@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.

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

1
2
3
4
5
6
7
8
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")

@Antheor

I always put my require functions at the beginning so I can use them wherever I want.

views:1442 update:2011/10/13 9:25:17
corona forums © 2003-2011