flash on 2015-9-24
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sNuj
*/
package {
import flash.events.MouseEvent;
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;
a = new xRect(); vecRect.push(a); a.cx = 200; a.cy = 200;
a = new xRect(); vecRect.push(a); a.cx = 300; a.cy = 200;
a = new xRect(); vecRect.push(a); a.cx = 100; a.cy = 150;
a = new xRect(); vecRect.push(a); a.cx = 50; a.cy = 60;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onMdown(e:MouseEvent):void { mbutton = 1;}
public function onMup(e:MouseEvent):void { mbutton = 0; }
public var mbutton:int = 0;
public var curOver:xRect = null
public var vecRect:Vector.<xRect>;
public function getMag(ax:Number, ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public function conRect(a:xRect, b:xRect, rest:Number, spec:int =0, t:Number=1.0):void
{
var mag:Number; var dx:Number; var dy:Number;
var imp:Number;
dx = a.cx-b.cx; dy= a.cy-b.cy;
mag = getMag(dx,dy);
if (mag == 0) { mag = 0.0001; }
if (spec == 1 && mag >= rest) { return; } //collision
dx /= mag; dy /= mag;
imp = mag - rest;
imp *= 0.5;
imp *= t;
a.cx += dx * -imp; a.cy += dy * -imp;
b.cx += dx * imp; b.cy += dy * imp;
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(b.cx,b.cy);
}//conrect
public function onEnter(e:Event):void
{
var mx:Number; var my:Number;
mx = stage.mouseX; my = stage.mouseY;
graphics.clear();
graphics.lineStyle(2, 0);
conRect(vecRect[0], vecRect[1], 50);
conRect(vecRect[1], vecRect[2], 70, 0.5);
conRect(vecRect[2], vecRect[3], 80, 0.25);
conRect(vecRect[0], vecRect[2], 32, 1);
conRect(vecRect[0], vecRect[3], 32, 1);
conRect(vecRect[1], vecRect[3], 32, 1);
var i:int; var num:int; var a:xRect;
num = vecRect.length;
for(i=0;i<num;i+=1)
{
a = vecRect[i];
graphics.drawCircle(a.cx,a.cy, 16);
if (mbutton == 1 && curOver == null && getMag(a.cx-mx, a.cy-my) < 16)
{ curOver = a; }
}//nexti
if (mbutton == 0) { curOver = null;}
if (curOver != null) { curOver.cx = mx; curOver.cy = my; }
}//onenter
}//classend
}
internal class xRect
{
public var cx:Number = 0;
public var cy:Number = 0;
}//xrect