forked from: forked from: ちりちりな何か
/**
* Copyright alexnotkin ( http://wonderfl.net/user/alexnotkin )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3Q28
*/
// forked from demouth's forked from: ちりちりな何か
// forked from demouth's ちりちりな何か
// forked from demouth's forked from: ひも的な何か
// forked from demouth's ひも的な何か
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
[SWF(frameRate='60')]
public class Main extends Sprite
{
protected var joints:Vector.<Vector.<Joint>> = new Vector.<Vector.<Joint>>();
protected var fric:Number = 0.01;
protected var dis:Number = 15;
protected var range:int = 7;//前後に影響を与える範囲
protected var num:int = 50;
protected var lineNum:int = 100;
public function Main()
{
if (this.stage) this.init()
else this.addEventListener(Event.ADDED_TO_STAGE , this.init);
}
protected function init(event:Event = null):void
{
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
for (var i:int = 0; i < this.lineNum; i++) this.joints.push(this.create());
this.addEventListener(Event.ENTER_FRAME , this.enterFrameHandler);
}
protected function create():Vector.<Joint>
{
var num:Number = this.num;
var joints:Vector.<Joint> = new Vector.<Joint>();
for (var i:int = 0; i < num; i++) joints.push(new Joint());
return joints;
}
private function enterFrameHandler(event:Event):void
{
this.graphics.clear();
for (var i:int = 0; i < this.lineNum; i++)
{
this.calc(this.joints[i]);
this.move(this.joints[i],i/this.lineNum);
this.draw(this.joints[i]);
}
}
private function calc(joints:Vector.<Joint>):void
{
var l:int = joints.length;
for (var i:int = 0; i < l; i++)
{
if (i == 0)
{
joints[0].x = this.stage.mouseX;
joints[0].y = this.stage.mouseY;
continue;
}
var b:int = this.range;
var a:int = this.range;
var now:Joint = joints[i];
for (var j:int = i-b; j < i+a+1; j++)
{
if (j >= 0 && l > j && j != i)
{
var t:Joint = joints[j];
var angle:Number = Math.atan2(now.y-t.y, now.x-t.x);
now.sx -= Math.cos(angle) * 6 * Math.random()*2 ;
now.sy -= Math.sin(angle) * 6 * Math.random()*2;
}
}
}
}
private function move(joints:Vector.<Joint> , spring:Number):void
{
var l:int = joints.length;
for (var i:int = 0; i < l; i++)
{
var now:Joint = joints[i];
if(i!=0)now.sy += 0.2;
now.x += now.sx * this.fric;
now.y += now.sy * this.fric;
if (i == 0) continue;
var b:Joint = joints[i-1];
var angle:Number = Math.atan2(b.y - now.y, b.x - now.x);
now.x = b.x - Math.cos(angle) * this.dis;
now.y = b.y - Math.sin(angle) * this.dis;
now.sx *= spring;
now.sy *= spring;
}
}
private function draw(joints:Vector.<Joint>):void
{
var g:Graphics = this.graphics;
var l:int = joints.length;
for (var i:int = 0; i < l; i++)
{
g.beginFill(0);
g.drawCircle(joints[i].x, joints[i].y, (i<7)?1:joints[i].sx*0.02+1);
g.endFill();
}
}
}
}
class Joint
{
public var x:Number = 0;
public var y:Number = 0;
public var sx:Number = 0;
public var sy:Number = 0;
}