topical media & game development

talk show tell print

actionscript-graph-basic-Character.ax

actionscript-graph-basic-Character.ax [swf] flex


  package  {
          import flash.display.*;
          import flash.text.TextField;
          import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.net.URLRequest;
          import flash.geom.Matrix;
          import flash.system.LoaderContext;
          public class @ax-actionscript-graph-basic-Character 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 id:String;                        //ID of the character
                  private var imageURL:String;                //Photo or image of the character
          private var spring:Number = 0.3;                //Movement of the characters
          private var xBeforeMoving:uint;                        //Movement of the characters
          private var yBeforeMoving:uint;                        //Movement of the characters
          private var friction:Number = 0.45;                //Movement of the characters
          public var vx:Number = 0;                                //Movement of the characters
          public var vy:Number = 0;                                //Movement of the characters
                  private var indexDisplay:int;        
                  var _loader:Loader = new Loader();
                  public function @ax-actionscript-graph-basic-Character (radius:Number, id:String, imageURL:String):void {                        
                          indexDisplay = 0;
                          this.radius=radius;
                          this.id=id;
                          this.imageURL=imageURL;
                          init(0, 0);                //Initialize the character (load photo and set it)
                  }
                  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);
                          _loader.load(new URLRequest(imageURL), loaderContext);                                                        //Load the photo and eventListener when completed
                          setX(x);
                          setY(y);
                  }
                  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);                                                                        //@ax-actionscript-graph-basic-Character 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();                                                                        //Clear the previous character
                          graphics.beginBitmapFill(bitmap,matrix);
                          graphics.drawCircle(0, 0, radius);                                        //Draw a bigger character over the previous one
                          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();                                                                        //Clear the bigger character
                          graphics.beginBitmapFill(bitmap,matrix);
                          graphics.drawCircle(0, 0, radius);                                        //Draw the normal sized character
                          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 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 - 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;
                  }
          }
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.