Water Particles
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import caurina.transitions.Tweener;
[SWF(backgroundColor="#FFFFFF", frameRate=40)]
public class FlashTest extends Sprite {
private var _particles:Array = [];
private var _label:TextField;
public function FlashTest() {
var rect :Sprite= new Rect( stage.stageWidth, stage.stageHeight );
addChild( rect );
rect.addEventListener( MouseEvent.CLICK, onClick );
rect.buttonMode = true;
rect.useHandCursor = true;
addEventListener( Event.ENTER_FRAME, onUpdate );
var t:TextField = new TextField();
t.mouseEnabled = false;
t.width = 200;
t.defaultTextFormat = new TextFormat("_sans", 12, 0x333366 );
t.text = "Please click to create particles.";
t.x = ( stage.stageWidth - t.width ) *0.5;
t.y = ( stage.stageHeight- t.height ) *0.5;
t.alpha = 0;
_label = t;
addChild( t );
Tweener.addTween( t, { alpha : 1, time:2, delay:0.3, transition:"easeInOutSine" } );
}
private function onClick( e:Event ) :void {
var num:uint = 30;
for( var i:int=0; i<num;++i ) {
var p:Particle = new Water( Math.random()* 10+3, 0, Math.random()*10+50, Math.random()*15+10, Math.random()*10-5 );
addChild(p);
_particles.push( p );
}
if( _label ) Tweener.addTween( _label, { alpha : 0, time:2, delay:0.3, transition:"easeInOutSine", onComplete: onRemoveLabel } );
}
private function onRemoveLabel ():void {
removeChild( _label );
_label = null;
}
private function onUpdate( e:Event ):void {
var num:uint = _particles.length;
var p:Particle;
for( var i:int=0; i<num;++i ) {
p = _particles[ i ] as Particle;
if( p.checkLife() ) {
p.update();
}
else {
if( contains(p) ) removeChild( p );
}
}
}
}
}
import flash.display.*;
class Particle extends Sprite {
public static const G:Number = 1;
public static const FRICTION_X:Number = 0.96;
public static const FRICTION_Y:Number = 0.96;
public var mass:Number=1;
public var vx:Number=0;
public var vy:Number=0;
public static var LIMIT_Y:Number = 500;
public function Particle ( smass:Number = 1, sx:Number=0, sy:Number = 0, svx:Number=0, svy:Number=0 ) {
mass = smass;
x = sx;
y = sy;
vx = svx;
vy = svy;
}
public function update():void {
vy += G;
vx *= FRICTION_X;
vy *= FRICTION_Y;
x += vx*mass;
y += vy*mass;
}
public function checkLife() :Boolean {
return y < LIMIT_Y;
}
}
class Water extends Particle {
public function Water( weight:Number, sx:Number=0, sy:Number = 0, svx:Number=0, svy:Number=0 ) {
super( weight*0.1, sx, sy, svx, svy );
graphics.beginFill( 0x6686ff, 0.5+Math.random()*0.4 );
graphics.drawCircle( 0, 0, weight );
graphics.endFill();
}
}
class Rect extends Sprite {
public function Rect( w:uint= 100, h:uint = 100 ) {
graphics.beginFill( 0, 0 );
graphics.drawRect( 0, 0, w, h );
graphics.endFill();
}
}