Particle Candy and directpr class

I have some problems trying to work with both director class and particle candy. Assuming I have a main page and some screens.lua. My folder organisation is :

mainfolder/lib_particle_candy.lua
mainfolder/main.lua (changing scene to home.lua)
mainfolder/screens/home.lua (navigation screen reaching screen1.lua)
mainfolder/screens/screen1.lua

If I want to run PC in one screen only :

  • where to put ?local Particles = require("../lib\_particle\_candy")
    in main.lua, in screen1.lua

  • what localGroup:insert()

Example :

Taking your Sample_Fading code, should it be :

  
module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 ------ Your code here ------  
  
 -- LOAD PARTICLE LIB  
 local Particles = require("../lib\_particle\_candy") -- HERE ?  
  
 local screenW = display.contentWidth  
 local screenH = display.contentHeight  
  
 local StatusText = display.newText( "", screenW/2, screenH-20, "Verdana-Bold", 12 )  
 StatusText:setTextColor( 255,255,255 )  
 -- DIRECTOR  
 localGroup:insert(StatusText)  
  
 -- CREATE AN EMITTER (NAME, SCREENW, SCREENH, ROTATION, ISVISIBLE, LOOP)  
 Particles.CreateEmitter("E1", screenW\*0.1, screenH\*0.8, 90, true, true)  
  
 -- DEFINE PARTICLE TYPE PROPERTIES  
 local Properties = {}  
 Properties.imagePath = "images/arrow.png"  
 Properties.imageWidth = 32 -- PARTICLE IMAGE WIDTH (newImageRect)  
 Properties.imageHeight = 32 -- PARTICLE IMAGE HEIGHT (newImageRect)  
 Properties.velocityStart = 150 -- PIXELS PER SECOND  
 Properties.autoOrientation = true -- ROTATE TO MOVEMENT DIRECTION  
 Properties.killOutsideScreen = true -- PARENT LAYER MUST NOT BE NESTED OR ROTATED!   
 Properties.lifeTime = 3000 -- MAX. LIFETIME OF A PARTICLE  
  
 Properties.alphaStart = 0 -- PARTICLE START ALPHA  
 Properties.fadeInSpeed = 0.5 -- PER SECOND  
 Properties.fadeOutSpeed = -0.75 -- PER SECOND  
 Properties.fadeOutDelay = 1500 -- WHEN TO START FADE-OUT  
  
 Particles.CreateParticleType ("Test1", Properties)  
  
 -- FEED EMITTERS (EMITTER NAME, PARTICLE TYPE NAME, EMISSION RATE, DURATION, DELAY)  
 Particles.AttachParticleType("E1", "Test1", 5, 99999,0)  
  
 -- DIRECTOR  
 localGroup:insert(E1) -- NOT SURE  
 localGroup:insert(Test1) -- NOT SURE  
 -- TRIGGER THE EMITTERS  
 Particles.StartEmitter("E1")  
 ----------------------------------------------------------------  
 -- MAIN LOOP  
 ----------------------------------------------------------------  
 local function main( event )  
  
 -- UPDATE PARTICLES  
 Particles.Update()  
  
 -- DISPLAY PARTICLE COUNT  
 StatusText.text = "PARTICLES:"..Particles.CountParticles()  
 end  
  
 -- timer.performWithDelay( 33, main, 0 )  
 Runtime:addEventListener( "enterFrame", main )  
 ----------------------------------------------------------------  
 -- END OF SAMPLE CODE  
 ----------------------------------------------------------------  
  
 ------ Home ------   
 local bt01 = display.newRect( 0, 10, 320, 20 )  
 bt01:setFillColor( 100,100,100 )  
 localGroup:insert(bt01)  
 local label1 = display.newText("HOME", 0, 0, native.systemFontBold, 16)  
 label1:setTextColor( 255,255,255)  
 label1.x = 160  
 label1.y = 20  
 localGroup:insert(label1)  
 local function bt01t ( event )  
 if event.phase == "ended" then  
  
 ----- Candy clean up before changing scene -----  
 CleanUp()   
 ----- Director -----   
 director:changeScene("samples/home","fade", "black")  
 end  
 end   
 bt01:addEventListener("touch",bt01t)  
  
 -- MUST return a display.newGroup()  
 return localGroup  
end  
  

Thx !
[import]uid: 9328 topic_id: 6123 reply_id: 306123[/import]

(wrong edit post)… [import]uid: 9328 topic_id: 6123 reply_id: 20963[/import]

No… Like this :

[code]
module(…, package.seeall)

– LOAD PARTICLE LIB
local Particles = require("…/lib_particle_candy") – HERE !

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

------ Your code here -----

local screenW = display.contentWidth
local screenH = display.contentHeight

local StatusText = display.newText( “”, screenW/2, screenH-20, “Verdana-Bold”, 12 )
StatusText:setTextColor( 255,255,255 )
– DIRECTOR
localGroup:insert(StatusText)

– CREATE AN EMITTER (NAME, SCREENW, SCREENH, ROTATION, ISVISIBLE, LOOP)
Particles.CreateEmitter(“E1”, screenW*0.1, screenH*0.8, 90, true, true)

– DEFINE PARTICLE TYPE PROPERTIES
local Properties = {}
Properties.imagePath = “images/arrow.png”
Properties.imageWidth = 32 – PARTICLE IMAGE WIDTH (newImageRect)
Properties.imageHeight = 32 – PARTICLE IMAGE HEIGHT (newImageRect)
Properties.velocityStart = 150 – PIXELS PER SECOND
Properties.autoOrientation = true – ROTATE TO MOVEMENT DIRECTION
Properties.killOutsideScreen = true – PARENT LAYER MUST NOT BE NESTED OR ROTATED!
Properties.lifeTime = 3000 – MAX. LIFETIME OF A PARTICLE

Properties.alphaStart = 0 – PARTICLE START ALPHA
Properties.fadeInSpeed = 0.5 – PER SECOND
Properties.fadeOutSpeed = -0.75 – PER SECOND
Properties.fadeOutDelay = 1500 – WHEN TO START FADE-OUT

Particles.CreateParticleType (“Test1”, Properties)

– FEED EMITTERS (EMITTER NAME, PARTICLE TYPE NAME, EMISSION RATE, DURATION, DELAY)
Particles.AttachParticleType(“E1”, “Test1”, 5, 99999,0)

– DIRECTOR
localGroup:insert(E1) – NOT SURE
localGroup:insert(Test1) – NOT SURE

– TRIGGER THE EMITTERS
Particles.StartEmitter(“E1”)


– MAIN LOOP

local function main( event )

– UPDATE PARTICLES
Particles.Update()

– DISPLAY PARTICLE COUNT
StatusText.text = “PARTICLES:”…Particles.CountParticles()
end

– timer.performWithDelay( 33, main, 0 )
Runtime:addEventListener( “enterFrame”, main )


– END OF SAMPLE CODE

------ Home ------
local bt01 = display.newRect( 0, 10, 320, 20 )
bt01:setFillColor( 100,100,100 )
localGroup:insert(bt01)
local label1 = display.newText(“HOME”, 0, 0, native.systemFontBold, 16)
label1:setTextColor( 255,255,255)
label1.x = 160
label1.y = 20
localGroup:insert(label1)
local function bt01t ( event )
if event.phase == “ended” then

----- Candy clean up before changing scene -----
CleanUp()
----- Director -----
director:changeScene(“samples/home”,“fade”, “black”)
end
end
bt01:addEventListener(“touch”,bt01t)

– MUST return a display.newGroup()
return localGroup
end[/code] [import]uid: 6981 topic_id: 6123 reply_id: 21200[/import]

Thx for info.
But I must do something else wrong, I can’t make it work. Anyway I’ll try to :wink:

The terminal tells me :


Runtime error
…rd/programmation/Corona/sandboxes/samples/candy1.lua:19: attempt to index global ‘Particles’ (a nil value)
stack traceback:

By the way, looking in demo codes I changed

localGroup:insert ("E1")

to

local Emitter = Particles.GetEmitter("E1") localGroup:insert (Emitter) [import]uid: 9328 topic_id: 6123 reply_id: 21273[/import]

ok. It sounds like “lib_particle_candy.lua” isn’t in the right place

try this .

local Particles = require(“lib_particle_candy”) [import]uid: 6981 topic_id: 6123 reply_id: 21368[/import]

When I put local Particles = require("…/lib_particle_candy") in screen1.lua I had “unable to find” message errors.
I tried local Particles = require(“lib_particle_candy”) with no more success.

Teh I tried to put local Particles = require(“lib_particle_candy” in my main.lua, and error message was different :
Runtime error
…rd/programmation/Corona/sandboxes/samples/candy1.lua:19: attempt to index global ‘Particles’ (a nil value)
stack traceback:

Is this message still a location error ? [import]uid: 9328 topic_id: 6123 reply_id: 21491[/import]

All the mess was coming from subdirectory organisation : I put all my lua on main folder and it worked !! [import]uid: 9328 topic_id: 6123 reply_id: 21529[/import]

Not sure it’s the right place to ask this :

Working on Events with :

-- DECLARE FUNCTION TO SEND EVENT TO WHEN PARTICLE GET DESTROYED  
-- NOTE: YOU CAN REMOVE THIS LISTENER AGAIN BY USING   
-- SetEmitterListener("name", nil)  
Particles.SetEmitterListener("E1", MyParticleDestroyListener)  

SO, before changing scene : I add

Particles.SetEmitterListener("E1", nil)

But, this line brings me the following error :
"
Runtime error
…/programmation/Corona/sandboxes/candy1.lua:147: attempt to call global ‘SetEmitterListener’ (a nil value)
stack traceback:
[C]: in function ‘SetEmitterListener’
…/programmation/Corona/sandboxes/candy1.lua:147: in function <…>
"

Any hints ? [import]uid: 9328 topic_id: 6123 reply_id: 21764[/import] </…>

Antheor, did you require the library at the root of your code (right on top of your main.lua)?

local Particles = require(“lib_particle_candy”)

You can test if “Particles” is available within any module using print(Particles) there, for example. If it says “nil”, the library is not accessible from that location of your code. It’s important where you load the libary into your code. If you do this in the root of your project, it should be accessible throughout your entire code.

[import]uid: 10504 topic_id: 6123 reply_id: 21775[/import]

I changed the position of the “require” line and it indeed worked fine, but I tried to change it back and it worked then …
Anyway, this problem is erratic and not interesting, mainly due to my poor coding knowledge.

I have but noticed a another behaviour, this time noticeable. I prepare a new post for it. [import]uid: 9328 topic_id: 6123 reply_id: 21811[/import]