package Chart { //Packages 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 Draw extends Sprite { private var radius:Number; //Radius of the circle shape character private var xWanted:uint; //X Coordinate destiny private var yWanted:uint; //Y Coordinate destiny private var quality:int; //Quality of the relation with the clicked element private var title:String; //Title of the character private var id:String; //ID of the character private var photo:String; //Photo or image of the character private var spring:Number = 0.3; private var xBeforeMoving:uint; private var yBeforeMoving:uint; private var friction:Number = 0.45; public var vx:Number = 0; public var vy:Number = 0; private var indexDisplay:int; var _loader:Loader = new Loader(); public function Draw (radious:Number, title:String, id:String, quality:int ,photo:String=""):void { indexDisplay = 0; this.quality = quality; this.radius=radious; this.title=title; this.id=id; this.photo=photo; init(0, 0); } public function getIndexDisplay():int { return indexDisplay; } public function setIndexDisplay(index:int):void { indexDisplay = index; } private function init(x:uint, y:uint):void { var loaderContext:LoaderContext = new LoaderContext(); loaderContext.checkPolicyFile=true; _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); addEventListener(MouseEvent.MOUSE_UP, onRelease); addEventListener(MouseEvent.MOUSE_DOWN, onRelease); _loader.load(new URLRequest(photo), loaderContext); //Load the photo and eventListener when completed setX(x); setY(y); } private function onPress(event:MouseEvent):void { event.target.startDrag(); } private function onRelease(event:MouseEvent):void { event.target.stopDrag(); } 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(); } 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; } private function setXBeforeMoving(x:uint):void { xBeforeMoving=x; } private function setYBeforeMoving(y:uint):void { yBeforeMoving=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 increaseSize(radius:uint):void { //When the mouse is over, increase the size 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); matrix.translate(radius,radius); graphics.clear(); graphics.beginBitmapFill(bitmap,matrix); graphics.drawCircle(0, 0, radius); graphics.endFill(); } public function decreaseSize():void { //When the mouse is out, normal size 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); matrix.translate(radius,radius); graphics.clear(); graphics.beginBitmapFill(bitmap,matrix); graphics.drawCircle(0, 0, radius); graphics.endFill(); } public function setXWanted (xWanted:uint) :void { //Set the destiny X this.xWanted = xWanted; setXBeforeMoving(x); } public function setYWanted (yWanted:uint) :void { //Set the destiny Y this.yWanted=yWanted; setYBeforeMoving(y); addEventListener(Event.ENTER_FRAME, onEnterFrame); } public function getYWanted ():uint { //Return the destiny position X return yWanted; } public function getXWanted():uint { //Return the destiny position Y return xWanted; } public function getQuality():int { //Get the quality relation with the clicked character return quality; } public function setQuality(quality:int):void { //Set the quality relation with the clicked character this.quality=quality; } public function getId(): String { //Return the identity of the character return id; } private function onEnterFrame(event:Event):void { //Move the character to the final destination // var dx:Number = xWanted+(xWanted-xBeforeMoving) - x; // var dy:Number = yWanted+(yWanted-yBeforeMoving) - x; /* var handle:animationch08ball = handles[i] as animationch08ball; var dx:Number = handle.x - ball.x; var dy:Number = handle.y - ball.y; ball.vx += dx * spring; ball.vy += dy * spring;*/ var dx:Number = xWanted + xWanted - xBeforeMoving - x; var dy:Number = yWanted + yWanted - yBeforeMoving - y; vx += dx * spring; vy += dy * spring; dx=xBeforeMoving-x; dy=yBeforeMoving-y; vx += dx * spring; vy += dy * spring; vx *= friction; vy *= friction; x += vx; y += vy; /* 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 } } }