package Chart { import flash.display.Sprite; import flash.text.TextField; import flash.display.Loader; import flash.events.Event; import flash.net.URLRequest; import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.Matrix; import flash.events.MouseEvent; import flash.system.LoaderContext; public class Relation extends Sprite { private var fromID:String; //Who makes the relation private var toID:String; //To whom is related private var quality:int; //Quality of the relation private var image:String; private var xWanted:uint; //X Coordinate destiny private var yWanted:uint; //Y Coordinate destiny private var radius:uint = 7; var _loader:Loader = new Loader(); // Also a description of the relation would be nice public function Relation(fromID:String, toID: String, quality:int, image:String=""): void { this.fromID = fromID; this.toID = toID; this.quality = quality; this.image = image; // init(0,0); } private function onComplete(event:Event):void { var bitmap:BitmapData = new BitmapData(_loader.width, _loader.height); bitmap.draw(_loader, new Matrix()); var matrix:Matrix = new Matrix(); matrix.scale(2*radius/_loader.width, 2*radius/_loader.height); //Set the matrix to translate the bitmap matrix.translate(radius,radius); graphics.beginBitmapFill(bitmap,matrix); //Fill the following shape with the bitmap graphics.drawCircle(0, 0, radius); //Draw the circle graphics.endFill(); } private function init(x:uint, y:uint):void { var loaderContext:LoaderContext = new LoaderContext(); loaderContext.checkPolicyFile=true; _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); _loader.load(new URLRequest(image), loaderContext); //Load the photo and eventListener when completed setX(x); setY(y); } public function getFromID():String { //Return the origin of the relation return fromID; } public function setFromID(fromID:String):void { //Set the origin of the relation this.fromID=fromID; } public function getToID():String { //Return the destination of the relation return toID; } public function setToID(toID:String):void { //Set the destination of the relation this.toID=toID; } public function getQuality():int { //Return the quality of the relation return quality; } public function setQuality(quality:int):void { //Set the quality of the relation this.quality=quality; } public function setX (x:uint) :void { //Set the actual position X this.x=x; } public function setY (y:uint) :void { //Set the actual position Y this.y=y; } public function getX ():uint { //Return the actual position X return this.x; } public function getY (): uint { //Return the actual position Y return this.y; } public function setXWanted (xWanted:uint) :void { //Set the destiny X this.xWanted = xWanted; } public function setYWanted (yWanted:uint) :void { //Set the destiny Y this.yWanted=yWanted; addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { //Move the character to the final destination if (xWanted > x) { x+=5; setX(x); } if (xWanted < x) { x-=5; setX(x); } if (yWanted > y) { y+=5 setY(y); } if (yWanted < y) { y-=5; setY(y); } if (((x>(xWanted-5)) && (x<(xWanted+5))) && ((y>(yWanted-5)) && (y<(yWanted+5)))) //When the characters are in their position removeEventListener(Event.ENTER_FRAME, onEnterFrame); //we remove the eventListener to save memory } } }