Emitting Particles with the Stardust particle engine


I never seem to get enough of particle generating systems. I just love them, the beautiful random shapes and patterns they make constantly keep me 'trippin'. Recently I've been experimenting with the Stardust particle system. Here is the first of my Stardust experiments. Click on the grey button to kick start things:-


package
{
import com.adobe.viewsource.ViewSource;

import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

import idv.cjcat.stardust.common.actions.Age;
import idv.cjcat.stardust.common.actions.CompositeAction;
import idv.cjcat.stardust.common.actions.DeathLife;
import idv.cjcat.stardust.common.actions.ScaleCurve;
import idv.cjcat.stardust.common.actions.triggers.DeathTrigger;
import idv.cjcat.stardust.common.clocks.SteadyClock;
import idv.cjcat.stardust.common.initializers.Life;
import idv.cjcat.stardust.common.initializers.Mask;
import idv.cjcat.stardust.common.initializers.Scale;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.Damping;
import idv.cjcat.stardust.twoD.actions.DeathZone;
import idv.cjcat.stardust.twoD.actions.Gravity;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.actions.Spawn;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.fields.UniformField;
import idv.cjcat.stardust.twoD.initializers.DisplayObjectClass;
import idv.cjcat.stardust.twoD.initializers.Position;
import idv.cjcat.stardust.twoD.initializers.Velocity;
import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
import idv.cjcat.stardust.twoD.zones.CircleZone;
import idv.cjcat.stardust.twoD.zones.LazySectorZone;
import idv.cjcat.stardust.twoD.zones.SinglePoint;


[SWF(width="640", height="360")]
public class Particle3 extends Sprite
{
private var _emit2d:Emitter2D;
private var _renderer:DisplayObjectRenderer;
private var _point:SinglePoint;
private var _emitting:Boolean = false;

public function Particle3()
{
ViewSource.addMenuItem(this, "srcview/index.html");

//
// create a 2D emitter
// the SteadyClock class emits 5 particles per frame every time the
// Emitter2D step() method is called
//
_emit2d = new Emitter2D(new SteadyClock(5));

// particle graphic
_emit2d.addInitializer(new DisplayObjectClass(DemoParticle));

//the starting velocity of the particle
_emit2d.addInitializer(new Velocity(new CircleZone(0, 0, 10)));

// the life span of the particle on stage
_emit2d.addInitializer(new Life(new UniformRandom(45, 75)));

// Spawn class allows for the creation of new particles after the parent particle ends its life
var spawn:Spawn = new Spawn(new UniformRandom(6, 8));
spawn.addInitializer(new Mask(2));
// give the spawner the display object is is to use as a particle
spawn.addInitializer(new DisplayObjectClass(DemoParticle1));
// set the life span of the spawned particles
spawn.addInitializer(new Life(new UniformRandom(10, 40)));
// assigne the velocity of the spawned particles
spawn.addInitializer(new Velocity(new LazySectorZone(5, 1)));
// assign a scale effect to the spawned particles
spawn.addInitializer(new Scale(new UniformRandom(1, 0.4)));

// create a gravity effect cfor the spawned particle
var gravity:Gravity= new Gravity();
gravity.addField(new UniformField(0, 0.075));

var commonActions:CompositeAction = new CompositeAction();
commonActions.mask = 1 | 2 | 4;
commonActions.addAction(gravity);
commonActions.addAction(new Age());
commonActions.addAction(new DeathLife());
commonActions.addAction(new Move());
commonActions.addAction(new ScaleCurve (0, 1));
commonActions.addAction(new Damping(0.01));
_emit2d.addAction(commonActions);

_point = new SinglePoint(stage.stageWidth * 0.5, stage.stageHeight * 0.5);

_emit2d.addInitializer(new Position(_point));
var trigger:DeathTrigger = new DeathTrigger();
trigger.mask = 1;
trigger.addAction(spawn);
//create the renderer

trace(stage.stageWidth * 0.5, stage.stageHeight * 0.5)
//add some actions that will make use of the settings
_emit2d.addAction(trigger)
_emit2d.addAction(new Age()); //reduces the life of the particle over time
_emit2d.addAction(new Move()); //makes the particle move based on its velocity
_emit2d.addAction(new DeathLife());
_emit2d.addAction(new DeathZone()); //removes particles from memory after use

_renderer = new DisplayObjectRenderer(this); //use stage as container for new particles
_renderer.addEmitter(_emit2d); //add emitter to renderer

var btnsprite:Sprite = new Sprite()
var btn:Shape = new Shape();
btn.graphics.lineStyle(6, 0x000000)
btn.graphics.beginFill(0x444444);
btn.graphics.drawCircle(stage.stageWidth/2 - 10, 20, 10);
btn.graphics.endFill();
btnsprite.addEventListener(MouseEvent.CLICK, onClickHandler)
btnsprite.buttonMode = true;
btnsprite.useHandCursor = true;
btnsprite.addChild(btn)
addChild(btnsprite);

//start rendering
}

private function renderLoop(e:Event):void
{
//update position of particle effect
_point.x = stage.stageWidth * 0.5
_point.y = stage.stageHeight * 0.5

//update emitter
_emit2d.step();
}

private function onClickHandler(e:Event):void
{
if (_emitting)
{
removeEventListener(Event.ENTER_FRAME, renderLoop);
_emitting = false;
}
else
{
addEventListener(Event.ENTER_FRAME, renderLoop);
_emitting = true;
}
}



}
}


0 comments:

My Instagram