Finer control for JSON files in emitter?

Hi,

I have a question regarding JSON files used for emitter objects in the graphics library.

I am trying to emulate colorful confetti effects when beating a level in my game app. Now, please keep in mind the colorful is the operative word here. If we have grey confetti…well, that’s just not very uplifting.

Ideally, each confetti particle will be either red, green, blue or yellow (and NOT black, white, or grey).

This is what my JSON file looks like:

{ "textureFileName":"json/confetti.png", "angle":90, "duration":-1, "sourcePositionVariancex":640, "sourcePositionVariancey":480, "speed":147, "gravityy":98, "rotationStart":0, "rotationStartVariance":0, "rotationEnd":720, "rotationEndVariance":49, "startColorRed":1, "startColorVarianceRed":1, "finishColorRed":1, "startColorGreen":1, "startColorVarianceGreen":1, "finishColorGreen":1, "startColorBlue":1, "startColorVarianceBlue":1, "finishColorBlue":1, "startColorAlpha":1, "finishColorAlpha":1, "particleLifespan":7, "startParticleSize":30, "finishParticleSize":30, "maxParticles":300, "blendFuncSource":1, "blendFuncDestination":771, }

Since I want the particles to take on colors such as red, green, blue, and yellow, I set startColorVarianceRed/Green/Blue to 1.

The problem with this is that sometimes (i.e. when R,G,B are nearly equal in value) the particles appear greyscale, which is quite depressing for a “victory” scene. 

What I want to achieve is for the particles to be Red 25%, Green 25%, Blue 25%, and Yellow 25% of the time, instead of having colors in between.

Is there any way to achieve this?

One way to do this is the create 4 separate json files, but having 4 files just for the 4 colors seems like a gross waste of resources including memory, which is very limited on mobile devices, so this approach is the absolute last resort.

Any solutions/ideas welcome.

Thanks in advance!

KC

Anyone?

Hi KC,

I see the dilemma here. This might be better achieved not through the JSON settings but instead through the Corona side of controlling color properties in a more fine-tuned way as the emitter runs. For example, when you start the confetti shooting out, you could start a looping timer that affects only the “startColor[]” properties of the particles (and you would also need to remove all of the “startColorVariance[]”, “finishColor[]” and “finishColorVariance[]” properties from the JSON too).

So, something roughly like this:

[lua]

math.randomseed( os.time() )

– Set up your emitter with your params, however you’re doing it

local emitter = display.newEmitter( emitterParams )

local function changeStartColor()

   local r = math.random( 4 )

   if r == 1 then  – Red confetti

      emitter.startColorRed = 1

      emitter.startColorBlue = 0

      emitter.startColorGreen = 0

   elseif r == 2 then  – Blue confetti

      emitter.startColorRed = 0

      emitter.startColorBlue = 1

      emitter.startColorGreen = 0

   elseif r == 3 then  – Green confetti

      emitter.startColorRed = 0

      emitter.startColorBlue = 0

      emitter.startColorGreen = 1

   elseif r == 4 then  – Yellow confetti

      emitter.startColorRed = 1

      emitter.startColorBlue = 0

      emitter.startColorGreen = 1

   end

end

– Sometime after you begin emitting particles, start repeating timer to change start color values every 20 milliseconds

– You’ll need to adjust the time increment to suit how fast you’re emitting particles, etc.

local colorChangeTimer = timer.performWithDelay( 20, changeStartColor, 0 )

[/lua]

Using something like this, you can see that it will never result in RGB values that are close enough to each other to produce a dull/grey confetti coloration. And, of course, you could expand this to create purple, orange, pink, or any number of other color options by just adding more random condition choices.

Hope this helps,

Brent

Hi Brent,

I did not know that can modify the emitter’s parameters (e.g. startColorRed, etc.) at Corona level!!

I just assumed that, since JSON is not written in Lua, all properties of a JSON file are immutable, and thus cannot be changed once created. This means everything!

As you hinted, there are many ways to go about this, but the solution you posted is really simple and should work just fine!

Thank you so much for your help!

KC

Anyone?

Hi KC,

I see the dilemma here. This might be better achieved not through the JSON settings but instead through the Corona side of controlling color properties in a more fine-tuned way as the emitter runs. For example, when you start the confetti shooting out, you could start a looping timer that affects only the “startColor[]” properties of the particles (and you would also need to remove all of the “startColorVariance[]”, “finishColor[]” and “finishColorVariance[]” properties from the JSON too).

So, something roughly like this:

[lua]

math.randomseed( os.time() )

– Set up your emitter with your params, however you’re doing it

local emitter = display.newEmitter( emitterParams )

local function changeStartColor()

   local r = math.random( 4 )

   if r == 1 then  – Red confetti

      emitter.startColorRed = 1

      emitter.startColorBlue = 0

      emitter.startColorGreen = 0

   elseif r == 2 then  – Blue confetti

      emitter.startColorRed = 0

      emitter.startColorBlue = 1

      emitter.startColorGreen = 0

   elseif r == 3 then  – Green confetti

      emitter.startColorRed = 0

      emitter.startColorBlue = 0

      emitter.startColorGreen = 1

   elseif r == 4 then  – Yellow confetti

      emitter.startColorRed = 1

      emitter.startColorBlue = 0

      emitter.startColorGreen = 1

   end

end

– Sometime after you begin emitting particles, start repeating timer to change start color values every 20 milliseconds

– You’ll need to adjust the time increment to suit how fast you’re emitting particles, etc.

local colorChangeTimer = timer.performWithDelay( 20, changeStartColor, 0 )

[/lua]

Using something like this, you can see that it will never result in RGB values that are close enough to each other to produce a dull/grey confetti coloration. And, of course, you could expand this to create purple, orange, pink, or any number of other color options by just adding more random condition choices.

Hope this helps,

Brent

Hi Brent,

I did not know that can modify the emitter’s parameters (e.g. startColorRed, etc.) at Corona level!!

I just assumed that, since JSON is not written in Lua, all properties of a JSON file are immutable, and thus cannot be changed once created. This means everything!

As you hinted, there are many ways to go about this, but the solution you posted is really simple and should work just fine!

Thank you so much for your help!

KC