ForceField
/**
* Copyright hrtsgt ( http://wonderfl.net/user/hrtsgt )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ujH6
*/
package{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.utils.*;
[SWF(width=465, height=465, backgroundColor=0x0, frameRate=60)]
public class ForceField extends Sprite{
public var _canvas:BitmapData;
public var ary:Array;
public var ct:ColorTransform = new ColorTransform( 1, 1, 1, 0.95, 0, 0, 0, 0);
public var chaser:Chaser;
public static const ROW:uint = 78;
public static const COL:uint = 78;
public function ForceField(){
stage.quality = StageQuality.LOW;
Wonderfl.capture_delay(3);
_canvas = new BitmapData( stage.stageWidth/2, stage.stageHeight/2, false, 0x0);
ary = [];
for(var i:uint=0; i<ROW; i++){
var _y:Number = i*3;
for(var j:uint=0; j<COL; j++){
var p:Particle = new Particle( j*3, _y);
ary.push(p);
}
}
chaser = new Chaser();
var bmp:Bitmap = addChild( new Bitmap(_canvas) ) as Bitmap;
bmp.scaleX = bmp.scaleY = 2;
this.addEventListener( Event.ENTER_FRAME, update);//
}
public function update( e:Event ):void{
chaser.chase( mouseX/2, mouseY/2);
var c:Point = new Point( chaser.x, chaser.y);
_canvas.lock();
_canvas.colorTransform( _canvas.rect, ct);
var t:Number = Math.cos(getTimer()/250)*30;
for(var i:uint=0; i<ary.length; i++){
var p:Particle = ary[i];
var d:Number = Point.distance( c, p.point());
var a:Number = Math.atan2( p.point().y - c.y, p.point().x - c.x);
var f:Number = (200 + t)/d;
p.force( Math.cos(a)*f, Math.sin(a)*f);//
_canvas.setPixel( p.x, p.y, 0xFFFFFF);
}
_canvas.unlock();
}
}
}
import flash.display.Shape;
import flash.geom.Point;
class Particle extends Shape{
private var px:Number;
private var py:Number;
private var ax:Number = 0;
private var ay:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
public function Particle( _x:Number, _y:Number){
x = px = _x;
y = py = _y;
}
public function point():Point{
return new Point( px, py);
}
public function force( _x:Number, _y:Number):void{
ax = (px - x)*0.5 + _x*0.9;
ay = (py - y)*0.5 + _y*0.9;
vx = (vx + ax)*0.9;
vy = (vy + ay)*0.9;
x += vx;
y += vy;
}
}
class Chaser extends Shape{
private var ax:Number = 0;
private var ay:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
public function chase( _mx:Number, _my:Number):void{
ax = (_mx - x)*0.1;
ay = (_my - y)*0.1;
vx = vx*0.9 + ax;
vy = vy*0.9 + ay;
x += vx*0.3;
y += vy*0.3;
}
}