package { import flash.display.Sprite; import Graph; import Node; import flash.events.*; import flash.utils.Timer; import flash.text.*; public class ScaleFreeSample extends Sprite{ private var graph:Graph; private var clusterConst:Number = .2; public function ScaleFreeSample(){ addMenu(); var tt:Timer = new Timer(800,30); graph = new Graph(); addChild(graph); var num:Number = 100; graph.createNode(100,100,{text:String(num)}); tt.addEventListener(TimerEvent.TIMER,function(ev:TimerEvent):void{ graph.stop(); (function():void{ var n:Node = scalefree(); num ++; var n2:Node = n.add(-1,-1,{ color : Math.floor(Math.random() * 0xffffff), text : num } ); var f:Function = function():void{ var n3:Node = graph.nodes[ Math.floor( Math.random() * graph.nodes.length )]; var n4:Node = scalefree(); if(! n3.isConnected(n4) ) n3.connect(n4); }; f(); })(); graph.move(); }); //tt.addEventListener(TimerEvent.TIMER_COMPLETE,graph.move); tt.start(); } public function addMenu():void{ return; var tf:TextField = new TextField(); tf.text = "[play again]"; addChild(tf); tf.addEventListener(MouseEvent.CLICK,this.playAgain); } public function playAgain(ev:MouseEvent):void{ graph.clear(); } public function scalefree():Node{ var p:Array = []; var weight:Object = {}; var out:Node; var last:Number = 0; for each(var n:Node in graph.nodes){ var t:Number = (1 + (clusterConst * n.edges.length)) * Math.random(); if(t > last){ out = n; last = t; } } return out; } } }