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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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

(wrong edit post)...

No.. Like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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

Thx for info.
But I must do something else wrong, I can't make it work. Anyway I'll try to ;)

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

1
2
local Emitter = Particles.GetEmitter("E1")
localGroup:insert (Emitter)

ok. It sounds like "lib_particle_candy.lua" isn't in the right place

try this .

local Particles = require("lib_particle_candy")

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 ?

All the mess was coming from subdirectory organisation : I put all my lua on main folder and it worked !!

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

Working on Events with :

1
2
3
4
-- 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)

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.

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.

views:2212 update:2011/10/14 9:11:21
corona forums © 2003-2011