引力・斥力
/**
* Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3z8a
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class FlashTest extends Sprite
{
public function FlashTest()
{
this.graphics.beginFill(0);
this.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private var head:Node;
private var tail:Node;
private function mouseDownHandler(e:MouseEvent):void
{
var node:Node=addNode();
addChild(node);
node.x=mouseX;
node.y=mouseY;
}
private function addNode():Node
{
var node:Node=new Node();
if(head==null)
{
node.prev=node.next=node;//
head=tail=node;
}
else
{
node.next=head;//
node.prev=tail;
tail=tail.next=node;
head.prev=tail;//
}
return node;
}
private var cnt:Number=0;
private function enterFrameHandler(e:Event):void
{
Node.equil=30+Math.sin((cnt++)/180*Math.PI*3)*20;
var node:Node=head;
do
{
node.update();
if(node.isDead)
{
if(node.prev==tail)
{
head=node.next;
tail.next=head;
}
else node.prev.next=node.next;
if(node.next==head)
{
tail=node.prev;
head.prev=tail;
}
else node.next.prev=node.prev;
removeChild(node);
}
node=node.next;
}
while(node!=head);
}
}
}
import flash.display.Sprite;
import frocessing.color.ColorHSV;
class Node extends Sprite
{
public function Node()
{
var color:ColorHSV=new ColorHSV(Math.random()*360);
this.graphics.beginFill(color.value);
this.graphics.drawCircle(0,0,10);
this.addChild(line=new Sprite());
}
private static const rev:Number=0.2;
public static var equil:Number=20;
public var prev:Node;
public var next:Node;
public var isDead:Boolean=false;
private var line:Sprite;
private var vx:Number=0;
private var vy:Number=0;
public function update():void
{
vx*=0.85; vy*=0.85;
var dis:Number;
if(prev!=null)
{
dis=Math.sqrt((prev.x-x)*(prev.x-x)+(prev.y-y)*(prev.y-y));
if(dis!=0)
{
vx+=(prev.x-x)/dis*(dis-equil)*rev;
vy+=(prev.y-y)/dis*(dis-equil)*rev;
}
}
if(next!=null)
{
dis=Math.sqrt((next.x-x)*(next.x-x)+(next.y-y)*(next.y-y));
if(dis!=0)
{
vx+=(next.x-x)/dis*(dis-equil)*rev;
vy+=(next.y-y)/dis*(dis-equil)*rev;
}
}
x+=vx; y+=vy;
if(x<0)
{
x*=-1;
vx*=-1;
}
else if(x>=stage.stageWidth)
{
x=stage.stageWidth*2-x;
vx*=-1;
}
if(y<0)
{
y*=-1;
vy*=-1;
}
else if(y>=stage.stageHeight)
{
y=stage.stageHeight*2-y;
vy*=-1;
}
if(prev!=null)
{
line.graphics.clear();
line.graphics.lineStyle(-1,0xFFFFFF);
line.graphics.moveTo(0,0);
line.graphics.lineTo(prev.x-x,prev.y-y);
}
if(false)isDead=true;
}
}