Blitting test (rotating squares)
Click to spawn squares.
EDIT: I cleared up the code a bit and added comments.
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
/**
* Click to spawn squares.
*
* EDIT: I cleared up the code a bit.
*/
public class Main extends Sprite {
private const WIDTH:int = stage.stageWidth;
private const HEIGHT:int = stage.stageHeight;
private var parts:Vector.<Particle>; // Vector for the particles
private var canvas:BitmapData = new BitmapData(WIDTH, HEIGHT, false, 0x000000); // The main canvas
private var partSpr:Sprite; // The sprite to draw to the canvas
private var bf:BlurFilter = new BlurFilter(8, 8, 1); // Setup blur filter
private var cmf:ColorMatrixFilter = new ColorMatrixFilter( // Setup color filter
[1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0.85, 0]
);
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
doStuff();
}
private var rad:Number;
private function doStuff():void {
rad = 6; // Base size of the square sprite
partSpr = new Sprite();
partSpr.graphics.beginFill(0xFFFFFF);
partSpr.graphics.drawRect(-rad, -rad, 2*rad, 2*rad);
partSpr.graphics.endFill();
parts = new Vector.<Particle>();
stage.addChild(new Bitmap(canvas));
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
}
// Mouse down handler //
private function mouseDown(e:MouseEvent):void {
addEventListener(Event.ENTER_FRAME, addParticles);
}
// Mouse up handler //
private function mouseUp(e:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, addParticles);
}
// Add particles //
private function addParticles(e:Event):void {
// Allocate variables
var X:Number, Y:Number, VX:Number, VY:Number, ROT:Number, ROTSPEED:Number, SCALE:Number;
var angle:Number, size:Number = 10*Math.random()+2;
var iters:int = 2;
// Spawn a total of 'iters' particles
for (var i:int = 0; i < iters; i++) {
// Set spawning point
X = stage.mouseX;
Y = stage.mouseY;
// Set the speed of magnitude 'size' in the direction 'angle'
angle = 2 * Math.PI * Math.random();
VX = size * Math.cos(angle);
VY = size * Math.sin(angle);
// Set rotation parameters
ROT = 2*Math.PI*Math.random();
ROTSPEED = Math.random()*0.3;
// Set the color (constraint: r+g+b = 1)
var r:Number = Math.random();
var g:Number = (1 - r) * Math.random();
var b:Number = 1 - r - g;
var CT:ColorTransform = new ColorTransform(r, g, b);
// Set scale
SCALE = Math.random()*.6 + .75;
// Create particle
parts.push( new Particle(X, Y, VX, VY, ROT, ROTSPEED, SCALE, CT) );
}
}
// Update canvas
private function enterFrame(e:Event):void {
canvas.lock();
// Apply filters
canvas.applyFilter(canvas, canvas.rect, new Point(0, 0), bf);
canvas.applyFilter(canvas, canvas.rect, new Point(0, 0), cmf);
const G:Number = 0.6;//0.327;
// Loop through all particles
for (var i:int; i < parts.length; ++i) {
var p:Particle = parts[i];
// Update parameters
p.x += p.vx;
p.y += (p.vy += G);
p.rot += p.rotSpeed;
p.vx *= 0.98;
p.vy *= 0.98;
// Remove if below the bottom edge of the stage
if (p.y > HEIGHT - rad*p.scale) {
if(p.atBottom) {
parts[i] = parts[parts.length - 1];
parts.length--;
--i;
} else {
p.vy *= -1;
p.y = HEIGHT - rad*p.scale;
p.atBottom = true;
}
} else p.atBottom = false;
// Bounce off sides
if ( p.x < 0 || p.x > WIDTH) {
p.vx = -p.vx;
p.x += 2*p.vx;
}
// Create transformation matrix
var M:Matrix = new Matrix();
M.scale(p.scale, p.scale);
M.rotate(p.rot);
M.translate(p.x, p.y);
// Draw particle
canvas.draw( partSpr, M, p.cT);
}
canvas.unlock();
}
}
}
import flash.geom.ColorTransform;
class Particle {
public function Particle(X:Number, Y:Number,
VX:Number, VY:Number,
ROT:Number, ROTSPEED:Number,
SCALE:Number, CT:ColorTransform
):void
{
x = X;
y = Y
vx = VX;
vy = VY;
rot = ROT;
rotSpeed = ROTSPEED;
scale = SCALE;
cT = CT;
}
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var rot:Number;
public var rotSpeed:Number;
public var cT:ColorTransform;
public var scale:Number;
public var atBottom:Boolean = false;
}