Event received ... not when we expect

The story : I try to understand PC and to make it work with director class. Beginnings were hard, but I now manage to do funny things.
I was working on events demo, Particles.SetEmitterListener("E1", MyParticleDestroyListener)
just putting :

1
2
3
4
5
6
7
        -- Count Particles destroyed
        local pDestroyed = 0
        local StatusText2 = display.newText( "", screenW/2, screenH-40, "Verdana-Bold", 12 )
        StatusText2:setTextColor( 255,255,255 )
        StatusText2.text = "destroyed : "..0    
        -- DIRECTOR
        localGroup:insert(StatusText2)

We noticed this behaviour in another user's project before (also while using Director Class). This could be solved, however, by restructuring the code a little bit. Could you send us a brief sample code please that demonstrates this issue (support -at- x-pressive.com)?

OK, glad to contribute :)

I clean up a little bit and send it.

Antheor, had a look to your code now.

You placed the particle stuff into the new() function. This is not recommended since Director puts EVERYTHING within this function into a display group (which is done "backstage"), making it really hard to remove it again. This can also lead to various unwanted side effects.

The best practice is to change scenes and THEN create any particles or emitters once the scene has changed. You should also remove all particle stuff BEFORE switching scenes again. In other words: particle stuff and Director functions should not be mixed together.

Doing it like this should keep you on the safe side:

- Load / change scene
- Create particle stuff
- Delete particle stuff
- Change scene

You did it like this:

- Load / change scene (creating particles here)
- Change scene

BTW: You placed Particles = require("lib_particle_candy") within a module. You should place it to the root of your code, right on top of your main.lua. When doing so, you should not use "local" to ensure that the library is accessible throughout your code and from all your modules (this is probably what caused your previous problem).

First, thx for the look at my code.

OK for the require at the root. For the rest, I'm not sure to understand.
Taking director template :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module(..., package.seeall)
 
-- Main function - MUST return a display.newGroup()
function new()
        local localGroup = display.newGroup()
        
        ------ Your code here ------
 
        -- I should ignore that and NOT put particles code here ?
        
        -- MUST return a display.newGroup()
        return localGroup
end
 
-- put my particles code here
--  with        localGroup:insert (Emitter) if needed

Yes, correct. Don't forget to remove all particle stuff before switching scenes again.

Got it :)

I'm sorry to ask but could update my support code to follow the advice you gave me on the forum.
I tried to copy/paste particles code out of director new() with no success.

I guess it has something to do with the following :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module(..., package.seeall)
 
-- Main function - MUST return a display.newGroup()
function new()
        local localGroup = display.newGroup()
        
        ------ Your code here ------
 
        -- OK, I ignore that and don't put particles code here, but
 
        -- HERE I have some local var ..........................
 
        -- MUST return a display.newGroup()
        return localGroup
end
 
-- put my particles code here
-- ......................THAT I use here ... : is that problematic ?
 
--  with        localGroup:insert (Emitter) if needed
views:1196 update:2011/10/14 9:11:21
corona forums © 2003-2011