flash on 2015-7-29
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/oLVx
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecRect = new Vector.<xRect>(0, false);
var a:xRect; var i:int;
for (i = 0; i < 16; i += 1)
{
a = new xRect();
var t:Number;
t = i * 0.4;
a.cx = 320+Math.cos(t)*160;
a.cy = 240 + Math.sin(t) * 160;
//a.cx = i*24; a.cy = 100;
a.xrad = 8 + i ;
a.xrad = 16;
vecRect.push(a);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecRect:Vector.<xRect>;
public function isInside(ax:Number, ay:Number, a:xRect):Boolean
{
if (ax < a.cx - (a.xrad)) { return false; } if (ax > a.cx + (a.xrad)) { return false; }
if (ay < a.cy - (a.xrad)) { return false; } if (ay > a.cy + (a.xrad)) { return false; }
return true;
}//isinside
public function isInside2(ax:Number, ay:Number, ar:Number, a:xRect):Boolean
{
if (ax < a.cx - (a.xrad+ar)) { return false; } if (ax > a.cx + (a.xrad+ar)) { return false; }
if (ay < a.cy - (a.xrad+ar)) { return false; } if (ay > a.cy + (a.xrad+ar)) { return false; }
return true;
}//isinside
public function getMag(ax:Number, ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public var gt:int = 0;
public function onEnter(e:Event):void
{
var mx:Number; var my:Number;
mx = stage.mouseX;
my = stage.mouseY;
graphics.clear();
graphics.lineStyle(2, 0);
var ang:Number;
var vx:Number; var vy:Number;
var a:xRect; var b:xRect;
var num:int; var i:int; var k:int;
var kx:Number; var ky:Number;
num = vecRect.length;
for (i = 0; i < num; i += 1)
{
a = vecRect[i];
if ((gt+i)% 40 ==0)
{ a.ang = Math.atan2(my-a.cy, mx-a.cx); }
ang = a.ang;
vx = Math.cos(ang) * 4;
vy = Math.sin(ang) * 4;
for (k = 0; k < num; k += 1)
{
if (i == k) { continue; }
b = vecRect[k];
kx = vx > 0 ? a.xrad : -a.xrad;
ky = vy > 0 ? a.xrad : -a.xrad;
/*
if (isInside(a.cx + kx, a.cy + ky, b))
{
if (isInside(a.cx + kx, a.cy, b)) { vx = 0; }
if (isInside(a.cx , a.cy+ky, b)) { vy = 0; }
}//endif
*/
/*
if (isInside2(a.cx + vx, a.cy + vy,a.xrad, b))
{
if (isInside2(a.cx + vx, a.cy,a.xrad, b)) { vx = 0; }
if (isInside2(a.cx , a.cy+vy,a.xrad, b)) { vy = 0; }
}//endif
*/
if (isInside2(a.cx + vx, a.cy + vy,a.xrad, b))
{
if (isInside2(a.cx + vx, a.cy,a.xrad*0.8, b)) { vx = 0; }
if (isInside2(a.cx , a.cy+vy,a.xrad*0.8, b)) { vy = 0; }
ang = Math.atan2(vy,vx);
}//endif
}//nextk
a.cx += vx; a.cy += vy;
graphics.drawRect(a.cx - a.xrad, a.cy - a.xrad, a.xrad * 2, a.xrad * 2);
graphics.drawCircle(a.cx, a.cy, a.xrad);
}//nexti
graphics.drawCircle(mx,my, 8);
gt += 1;
}//onenter
}//classend
}
internal class xRect
{
public var cx:Number = 0;
public var cy:Number = 0;
public var xrad:Number = 16;
public var ang:Number = 0;
}//xrect