package { import flash.display.*; import flash.events.*; import flash.filters.*; [SWF(width="400", height="400", backgroundColor="0xffffff", frameRate="8")] /** * @author : Mateusz Malczak (http://segfaultlabs.com) */ public class Main extends MovieClip { private var bg : BitmapData = new BitmapData(400,400); private var flies : Vector.<Fly> = new Vector.<Fly>(0,false); private var adja : ColorMatrixFilter; public function Main():void { var i:int; var f:Fly; var b:Bitmap = new Bitmap( bg ); addChild( b ); for ( i=0; i<40; i+=1 ) { f = new Fly(); addChild( f ); f.x = (350*Math.random()+25); f.y = (350*Math.random()+25); flies.push( f ); }; adja = new ColorMatrixFilter([ 1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,0.6,0 ] ); addEventListener( Event.ENTER_FRAME, handleFlies ); }; private function handleFlies( evt:Event ):void { bg.applyFilter( bg, bg.rect, bg.rect.topLeft, adja ); flies.forEach( handler ); }; private function handler( obj:Fly, idx:int, v:Vector.<Fly> ):void { obj.handle(); bg.draw( obj, obj.transform.matrix ); }; } } import flash.display.Shape; import flash.filters.*; import flash.geom.*; final class Fly extends Shape { private static var maxT:Number = 30; private static var vMIN:Number = 30; private static var vMAX:Number = 90; private static var wallV:Number = 100; private var t:Number; private var sp:Number; private var v:Point; private var vb:Point; private var nn:Number; private var alfa:Number; public function Fly():void { vb = new Point(); vb.x = 0; vb.y = 0; v = new Point(); v.x = 0; v.y = 0; alfa = 0; t = maxT*Math.random(); nn = 50; sp = (vMAX-vMIN)*Math.random()+vMIN; v.x = Math.random(); v.y = Math.random(); if ( Math.random()<0.5 ) v.x = -v.x; if ( Math.random()<0.5 ) v.y = -v.y; orient(); graphics.beginFill(0xab3a11); graphics.moveTo(0,0); graphics.lineTo(10,10); graphics.lineTo(-10,10); graphics.endFill(); graphics.beginFill(0xab3a11); graphics.drawRect(-2,8,4,8); graphics.endFill(); filters = [ new BlurFilter(0,4), new GlowFilter() ]; } public function randV():void { var vm:Number = vMAX-vMIN; sp = (vMAX-vMIN)*Math.random()+vMIN;; var x:Number = Math.random(); var y:Number = Math.random(); if ( Math.random()<0.5 ) x = -x; if ( Math.random()<0.5 ) y = -y; vb.x = v.x; vb.y = v.y; vm = Math.sqrt( vb.x*vb.x + vb.y*vb.y ); x /= vm; y /= vm; v.x = x * vb.x + y * vb.y; // || do v v.y = -x * vb.y + y * vb.x; // _|_ do v alfa = Math.atan2( v.y, v.x ) / 20; v.x = vb.x; v.y = vb.y; orient(); t = maxT+Math.random()*maxT; nn = 1; alpha = 0.20 + 0.80*Math.random(); }; public function orient():void { rotation = Math.atan2(v.y,v.x)*180/Math.PI+90; }; public function handle():void { var vx:Number; var vy:Number; if (nn<20) { vx = Math.cos(alfa*nn); vy = Math.sin(alfa*nn); v.x = vb.x*vx + vb.y*vy; v.y = -vb.x*vy + vb.y*vx; vy = sp/20*nn; vx = v.x*vy; vy = v.y*vy; orient(); nn += 2; } else { vx = v.x*sp; vy = v.y*sp; }; t -= 1; if ( t<0 ) randV(); if ( alpha<100 ) alpha += 0.2; vx = vx - wallV/Math.sqrt(stage.stageWidth-1-x); vx = vx + wallV/Math.sqrt(x); vy = vy - wallV/Math.sqrt(stage.stageHeight-1-y); vy = vy + wallV/Math.sqrt(y); x += vx*0.1; y += vy*0.1; if ( x<0 || x>stage.stageWidth ) x = stage.stageWidth/2; if ( y<0 || y>stage.stageHeight ) y = stage.stageHeight/2; } } trapped