Mouse repulsion
/**
* Copyright ram64 ( http://wonderfl.net/user/ram64 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fwlS
*/
package {
import flash.filters.BlurFilter;
import flash.events.MouseEvent;
import flash.display.Sprite;
import com.flashdynamix.motion.*;
import net.hires.debug.Stats;
[SWF(backgroundColor="0x131313")]
public class FlashTest extends Sprite {
private var _grid:Array = [];
private var _rows:Number = 60;
private var _columns:Number = 60;
private var _bf:BlurFilter = new BlurFilter(3,3, 2);
public function FlashTest() {
// write as3 code here..
var stats:Stats = new Stats();
addChild(stats);
createField();
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void
{
var dx:Number, dy:Number, dist:Number;
var mx:Number = mouseX;
var my:Number = mouseY;
var m:Molecule;
var maxDist:Number = 100//Math.random()*20+80;
var s:Number;
for(var i:int; i<_grid.length; i++)
{
var nx:Number, ny:Number;
m = _grid[i];
dx = m.x-mx;
dy = m.y-my;
dist = Math.sqrt(dx *dx + dy*dy);
if(dist < maxDist){
var o:Number = Math.atan2(dy, dx);
var rd:Number = 50+Math.random()*40;
nx = m.ox+Math.cos(o)*rd;
ny = m.oy+Math.sin(o)*rd;
m.filters = [_bf];
s = 3 - dist/maxDist;
Tweensy.to(m,{x:nx, y:ny, scaleX:s, scaleY:s}, 1, null, 0 , null, goBack, [m]);
}
}
}
private function goBack(m:Molecule):void
{
Tweensy.to(m, {x:m.ox, y:m.oy, scaleX:1, scaleY:1});
m.filters = null;
}
private function createField():void
{
var xDist:Number = 465/_columns;
var yDist:Number = 465/_rows;
var m:Molecule;
for(var i:Number = 0; i<_rows; i++)
{
for(var j:Number = 0; j<_columns; j++)
{
m = new Molecule(0xff00ff);
m.x = m.ox = j*xDist;
m.y = m.oy = i*yDist;
addChild(m);
_grid.push(m);
}
}
}
}
}
import flash.display.Sprite;
class Molecule extends Sprite
{
public var ox:Number;
public var oy:Number;
public function Molecule(color:uint)
{
graphics.beginFill(color);
graphics.drawCircle(0, 0, 1);
graphics.endFill();
}
}