Apply color to particles

19 views
Skip to first unread message

Gen

unread,
Apr 30, 2010, 5:57:00 AM4/30/10
to Stardust Particle Engine
hi all,
first of all thanks for sharing this great particle engine!

is there anyway to dynamically apply color to a particles?
I thought Color class would set default color to the particle but
seems it doesn't.
ie) addInitializer(new Color(0xFF0000));

I know I can create classes with different colors and pass it to
SwitchInitializer,
but what if i want to randomize each color applied to a particle?

thanks,
gen

CJ Cat (Allen Chou)

unread,
Apr 30, 2010, 9:19:02 AM4/30/10
to Stardust Particle Engine
Ah, the Color initializer is for initializing the Particle.color
property, not the display object's color transform.
If you want to create random colors for your particle display objects,
one easy way to do so is to initialize the color in your display
object's constructor.

Or you can extend your own initializer that assigns a color transform
to the display objects. But be sure to set your initializer's priority
to a value lower than that of the DisplayObjectClass initializer, so
that the Particle.target property is not null when you access it in
your own initializer.

P.S. The DisplayObjectClass initializer instantiates display objects
and then assign them to the Particle.target property, which is used by
the DisplayObjectRenderer to update the display objects according to
the particle data.

Gen

unread,
May 9, 2010, 9:28:20 PM5/9/10
to Stardust Particle Engine
Thanks CJ.
Got the idea.

(sorry for the late reply, been off for week)

chkd...@googlemail.com

unread,
Jun 2, 2010, 3:39:25 AM6/2/10
to Stardust Particle Engine
i use a randomcolor initializer to generate a particle color out of 3.
and the third color should be more:


package {

import idv.cjcat.stardust.common.initializers.Color;
import idv.cjcat.stardust.common.particles.Particle;

public class RandomColor extends Color {

private var Random:Number = 0;
private var cColor:uint;
public function RandomColor():void
{


}

override public function initialize(particle:Particle):void {
Random = int(Math.random()*5)
switch (Random)
{
case 0:
cColor = 0xff0000;
break;
case 1:
cColor = 0x00ff00;
break;
case 2:
case 3:
case 4:
cColor = 0x00ffff;
break;

}
particle.color = cColor;
//particle.color = 0x0;

Gen

unread,
Jun 2, 2010, 11:47:08 PM6/2/10
to Stardust Particle Engine
Hi chkdesign,

this is what i came up with.
not exactly random but decided to set certain colors and weight them
just like the SwitchInitializer class.

package {
import flash.display.DisplayObject;
import flash.geom.ColorTransform;
import idv.cjcat.stardust.common.particles.Particle;
import idv.cjcat.stardust.common.utils.WeightedCollection;
import idv.cjcat.stardust.common.xml.XMLBuilder;

public class Colors extends Initializer {

private var _collection:WeightedCollection;

/**
*
* @param colors
* @param weights
*/
public function Colors(colors:Array = null, weights:Array = null) {
if (!colors) colors = [];
if (!weights) {
var len:int = colors.length;
var weights:Array = [];
for (var i:int = 0; i < len; i++) weights[i] = 1;
}

setColors(colors, weights);
}

public function setColors(colors:Array = null, weights:Array =
null):void
{
_collection = new WeightedCollection(colors, weights);
}

/**
*
* @param particle
*/
override public function initialize(particle:Particle):void
{
var color:uint = _collection.get() as uint;
var ct:ColorTransform = new ColorTransform();
ct.color = color;
(particle.target as DisplayObject).transform.colorTransform = ct;
}

//XML
//------------------------------------------------------------------------------------------------
...

//------------------------------------------------------------------------------------------------
//end of XML
}
}


usage would be...
addInitializer(new Colors([0xcce28f, 0x79a81c, 0xc9afd4, 0x75587b],
[1, 1, 1, 1]));


On Jun 2, 4:39 pm, "chkdes...@googlemail.com"
Reply all
Reply to author
Forward
0 new messages