forked from: 真似してみた
もとねたはこちらhttp://amaznode.fladdict.net/about_ja.html
動きはそれっぽい?
本家は微調整がすごそう
// forked from velvet1113's 真似してみた
package
{
//もとねたはこちらhttp://amaznode.fladdict.net/about_ja.html
//動きはそれっぽい?
//本家は微調整がすごそう
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite
{
private const NUM:int = 20;
private const SPRING:Number = 0.35;
private const FRICTION:Number = 0.2;
private var draggingNode:Node;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
NodeManager.init();
var timer:Timer = new Timer(3000 , NUM);
timer.addEventListener(TimerEvent.TIMER , createNode);
timer.start();
addEventListener(Event.ENTER_FRAME , enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_UP , dragStop);
}
private function createNode(e:TimerEvent):void {
var node:Node = new Node();
node.x = Math.random() * stage.stageWidth;
node.y = Math.random() * stage.stageHeight;
node.addEventListener(MouseEvent.MOUSE_DOWN , dragStart);
NodeManager.add(node);//NodeManagerに登録
addChild(node);
}
private function dragStart(e:MouseEvent):void {
e.target.startDrag();
draggingNode = e.target as Node;
draggingNode.dragging = true;
}
private function dragStop(e:MouseEvent):void {
if (draggingNode) {
draggingNode.stopDrag();
draggingNode.dragging = false;
draggingNode = null;
}
}
private function enterFrameHandler(e:Event):void {
var nodeSetList:Array = NodeManager.nodeSetList;
graphics.clear();
for (var i:int = 0 ; i < nodeSetList.length ; i ++ ) {
var node1:Node = nodeSetList[i].nodes[0];
var node2:Node = nodeSetList[i].nodes[1];
if(!node2.dragging) springTo(node1 , node2 , nodeSetList[i].strength);
if (!node1.dragging) springTo(node2 , node1 , nodeSetList[i].strength);
var color:uint;
if (node1.dragging || node2.dragging) {
color = 0x0066CC;
}else {
color = 0xCCCCCC;
}
drawLine(node1 , node2 , color);
}
}
private function springTo(_node1:Node , _node2:Node , offset:int):void {
var nodeA:Node = _node1;
var nodeB:Node = _node2;
var dx:Number = nodeA.x -nodeB.x;
var dy:Number = nodeA.y -nodeB.y;
var angle:Number = Math.atan2(dy , dx);
var targetX:Number = nodeA.x - Math.cos(angle) * offset;
var targetY:Number = nodeA.y - Math.sin(angle) * offset;
nodeB.vx += (targetX - nodeB.x) * SPRING;
nodeB.vy += (targetY - nodeB.y) * SPRING;
nodeB.vx *= FRICTION;
nodeB.vy *= FRICTION;
nodeB.x += nodeB.vx;
nodeB.y += nodeB.vy;
}
private function drawLine(_node1:Node , _node2:Node , _color:uint):void {
graphics.lineStyle(1 , _color);
graphics.moveTo(_node1.x , _node1.y);
graphics.lineTo(_node2.x , _node2.y);
}
}
}
class NodeManager
{
public static var nodeList:Array;//ノードを格納する配列
public static var nodeSetList:Array;//結びついている1対のノードの情報
public static function init():void {
nodeList = new Array();
nodeSetList = new Array();
}
public static function add(node:Node):void {
nodeList.push(node);
addNodeSet();
}
private static function addNodeSet() :void{
//今回はランダムに決める
var len:int = nodeList.length;
if (len > 1) {
for (var i:int = 0 ; i < len - 1; i++ ) {
if (Math.random() > 0.7) {
var nodeSet:Object = {
nodes: [nodeList[i] , nodeList[len - 1]] ,//結びつく2つのノード
strength: Math.floor(Math.random() * 50 + 80)//ノード間の距離
};
nodeSetList.push(nodeSet);
}
}
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
class Node extends Sprite
{
public var vx:Number = 0;
public var vy:Number = 0;
private var w:Number = 30;
private var h:Number = 40;
private var scale:Number = 1.2;
public var dragging:Boolean = false;
public function Node() {
graphics.lineStyle(3 , 0x000000);
graphics.beginFill(0xFFFFFF , 1);
graphics.drawRect(-w/2 , -h/2 , w , h);
graphics.endFill();
buttonMode = true;
addEventListener(MouseEvent.MOUSE_OVER , mouseOver);
addEventListener(MouseEvent.MOUSE_OUT , mouseOut);
addEventListener(Event.ENTER_FRAME , move);
}
private function move(e:Event):void {
if (Math.random() > 0.7) {
this.x += Math.random() * 2 - 1;
this.y += Math.random() * 2 - 1;
}
}
private function mouseOver(e:MouseEvent):void {
w *= scale;
h *= scale;
graphics.clear();
graphics.lineStyle(3 , 0x0066CC);
graphics.beginFill(0xFFFFFF , 1);
graphics.drawRect( -w / 2 , -h / 2 , w , h);
graphics.endFill();
}
private function mouseOut(e:MouseEvent):void {
w /= scale;
h /= scale;
graphics.clear();
graphics.lineStyle(3 , 0x000000);
graphics.beginFill(0xFFFFFF , 1);
graphics.drawRect( -w / 2 , -h / 2 , w , h);
graphics.endFill();
}
}