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 :

-- 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)
and in MyParticleDestroyListener function : pDestroyed = pDestroyed + 1 StatusText2.text = "destroyed :"..pDestroyed
to count destroyed particles.

I navigate between two screens, just before the screen change, I have ----- Candy clean up before changing scene ----- Particles.SetEmitterListener("E1", nil) Emitter = nil Particles.CleanUp() Runtime:removeEventListener( "enterFrame", main ) ----- Director ----- director:changeScene("home","fade", "black")

I first realized that when i change screens (screen with particles - home screen - back to screen with particles), the pDestroyed was not 0 …

Looking at the terminal I have :
When I quit screen with particles to home screen :
"
–> Particles.ClearParticles()
–> Particles.DeleteParticleType(): REMOVING ‘Test1’ FROM EMITTER ‘E1’
–> Particles.DeleteParticleType(): DELETED PARTICLE TYPE ‘Test1’
–> Particles.DeleteEmitter(): DELETED EMITTER ‘E1’
–> Particles.CleanUp(): FINISHED.
"
When I click back to screen with particles :
EVENT RECEIVED: particleKilled
EVENT RECEIVED: particleKilled
EVENT RECEIVED: particleKilled
… many times …"

Wow ! I thought I destroyed the listeners when I quit ! It’s still there when I’m back …hm hm
I even realized (waiting on home screen before returning back to particles screens) that the more I wait the more event received fire (and pDestroyed increases) …

Is that a nice problem ? [import]uid: 9328 topic_id: 6317 reply_id: 306317[/import]

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)? [import]uid: 10504 topic_id: 6317 reply_id: 21913[/import]

OK, glad to contribute :slight_smile:

I clean up a little bit and send it. [import]uid: 9328 topic_id: 6317 reply_id: 21920[/import]

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). [import]uid: 10504 topic_id: 6317 reply_id: 21940[/import]

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 :

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  
  

That’s it ? [import]uid: 9328 topic_id: 6317 reply_id: 21947[/import]

Yes, correct. Don’t forget to remove all particle stuff before switching scenes again. [import]uid: 10504 topic_id: 6317 reply_id: 22072[/import]

Got it :slight_smile: [import]uid: 9328 topic_id: 6317 reply_id: 22186[/import]

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 :

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  
  

Thx for any hints ? [import]uid: 9328 topic_id: 6317 reply_id: 22241[/import]