Simple Flint Test from Tutorial
Walking through the snowflake tutorial for the Flint particle system at http://flintparticles.org/tutorials/snowfall.
I've done my best to add helpful comments to the code for anyone that wants to fork this and start playing around.
♥0 |
Line 41 |
Modified 2010-11-02 01:16:25 |
MIT License
archived:2017-03-10 21:58:23
ActionScript3 source code
/**
* Copyright dot.tominator ( http://wonderfl.net/user/dot.tominator )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hOdD
*/
package {
[SWF(backgroundColor="#000000")]
import flash.display.Sprite;
public class FlintTest extends Sprite {
public function FlintTest() {
// Testing the Flint particle library
import flash.geom.Point;
import org.flintparticles.common.counters.*;
import org.flintparticles.common.displayObjects.RadialDot;
import org.flintparticles.common.initializers.*;
import org.flintparticles.twoD.actions.*;
import org.flintparticles.twoD.emitters.Emitter2D;
import org.flintparticles.twoD.initializers.*;
import org.flintparticles.twoD.renderers.*;
import org.flintparticles.twoD.zones.*;
// Couldn't get the SWF metadata to work, so need to
// draw a black box over our background. I guess.
graphics.beginFill(0x000000);
graphics.drawRect(0,0, stage.stageWidth, stage.stageHeight);
// Create the renderer.
// This one is a simple DisplayObject renderer
var renderer:DisplayObjectRenderer = new DisplayObjectRenderer();
addChild(renderer);
// Create the emitter
var emitter:Emitter2D = new Emitter2D();
// ..and add the emitter to the renderer
renderer.addEmitter(emitter);
// The counter tells the emitter how, and how often,
// to create new particles
emitter.counter = new Steady(100);
// INITIALIZERS...
// Create an ImageClass instance to hold the DisplayObject
// definition for each particle created.
// Here, we're using the RadialDot image class which takes
// the pixel radius as its parameter.
var snowflakeImageInitializer:ImageClass = new ImageClass(RadialDot, 2);
emitter.addInitializer(snowflakeImageInitializer);
// Create a Zone to hold the starting position for each new
// snowflake particle as it is created.
var snowflakeZone:LineZone = new LineZone(new Point(-5, -5), new Point(500, -5));
// Pass this zone to the position initializer for the emitter
var snowflakePositionInitializer:Position = new Position(snowflakeZone);
emitter.addInitializer(snowflakePositionInitializer);
// Create a new Velocity initializer, which defines the vector for each
// particle. The vector is (0,0) => (x,y) and describes the distance
// each particle covers in 1 second.
var snowflakeVector:PointZone = new PointZone (new Point(0, 50));
var snowflakeVelocityInitializer:Velocity = new Velocity(snowflakeVector);
emitter.addInitializer(snowflakeVelocityInitializer);
// Create a ScaleImage initializer that randomly scales each particle
// as it's being created.
var snowflakeRandomScale:ScaleImageInit = new ScaleImageInit(.75, 2);
emitter.addInitializer(snowflakeRandomScale);
// ACTIONS...
// Get the little suckers updating. Each "Action" is executed once per particle
// per timer event. This basic action moves each particle according to its
// velocity initializer.
emitter.addAction(new Move());
// We need to make sure these particles "die" or else we will quickly
// bog down the Player. This DeathZone is actually the "live" zone -
// particles that fall outside this zone will be automatically destroyed.
var snowflakeLiveArea:RectangleZone = new RectangleZone(-10, -10, stage.stageWidth + 10, stage.stageHeight + 10);
var snowflakeDeathZone:DeathZone = new DeathZone(snowflakeLiveArea, true);
emitter.addAction(snowflakeDeathZone);
// RandomDrift modifies the movement vector for each particle.
// It doesn't seem to change on each update, so I'm not sure why this is an Action
// and not an initializer. I have a lot to learn...
emitter.addAction(new RandomDrift(10, 10));
// All done.... start the emitter!
emitter.start();
// ...and pre-roll the animation to 10 seconds so that the particles
// are already filling the screen when we start.
emitter.runAhead(10);
}
}
}