home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Jumper.swf / scripts / gamegraphics / GraphicsObject.as < prev    next >
Encoding:
Text File  |  2008-09-05  |  4.8 KB  |  163 lines

  1. package gamegraphics
  2. {
  3.    import flash.display.Bitmap;
  4.    import flash.display.BitmapData;
  5.    import flash.display.Sprite;
  6.    import flash.events.Event;
  7.    import flash.geom.Point;
  8.    import flash.geom.Rectangle;
  9.    import flash.utils.Timer;
  10.    import game.GameObject;
  11.    import game.GraphicsChangedEvent;
  12.    import game.ObjectChangedEvent;
  13.    
  14.    public class GraphicsObject
  15.    {
  16.        
  17.       
  18.       private var sprite:Sprite;
  19.       
  20.       private var w:int;
  21.       
  22.       private var overWidth:int;
  23.       
  24.       private var defaultLoop:Boolean;
  25.       
  26.       private var image:Bitmap;
  27.       
  28.       private var timer:Timer;
  29.       
  30.       private var defaultImage:Bitmap;
  31.       
  32.       private var mainImage:BitmapData;
  33.       
  34.       private var loop:Boolean;
  35.       
  36.       private var frames:int = 1;
  37.       
  38.       private var h:int;
  39.       
  40.       private var overHeight:int;
  41.       
  42.       private var frame:int = 0;
  43.       
  44.       private var obj:GameObject;
  45.       
  46.       private var defaultFrameDelay:int;
  47.       
  48.       public function GraphicsObject(obj:GameObject, imageClass:Class, frameDelay:int = 0, loop:Boolean = false, overWidth:int = 0, overHeight:int = 0)
  49.       {
  50.          super();
  51.          this.obj = obj;
  52.          this.overWidth = overWidth;
  53.          this.overHeight = overHeight;
  54.          defaultImage = new imageClass();
  55.          defaultFrameDelay = frameDelay;
  56.          defaultLoop = loop;
  57.          image = new Bitmap();
  58.          initImage(defaultImage,frameDelay,loop);
  59.          updateImage();
  60.          refresh(null);
  61.          GameGraphics.sprite.addChild(image);
  62.          obj.addEventListener("objectChanged",refresh);
  63.          obj.addEventListener("objectDestroyed",destroy);
  64.          obj.addEventListener("graphicsChanged",changeGraphics);
  65.       }
  66.       
  67.       public function move(event:ObjectChangedEvent) : void
  68.       {
  69.          image.x = event.x;
  70.          image.y = event.y;
  71.       }
  72.       
  73.       public function refresh(event:Event) : void
  74.       {
  75.          image.width = obj.w + overWidth;
  76.          image.height = obj.h + overHeight;
  77.          image.x = obj.x - overWidth / 2;
  78.          image.y = obj.y - overHeight / 2;
  79.       }
  80.       
  81.       private function changeFrame(event:Event) : void
  82.       {
  83.          ++frame;
  84.          if(frame >= frames)
  85.          {
  86.             if(┬ºgamegraphics:GraphicsObject┬º.loop == true)
  87.             {
  88.                frame = 0;
  89.             }
  90.             else
  91.             {
  92.                mainImage.dispose();
  93.                mainImage = defaultImage.bitmapData;
  94.                w = defaultImage.width;
  95.                h = defaultImage.height;
  96.                frames = ┬ºgamegraphics:GraphicsObject┬º.w / ┬ºgamegraphics:GraphicsObject┬º.h;
  97.                initImage(defaultImage,defaultFrameDelay,defaultLoop);
  98.             }
  99.          }
  100.          updateImage();
  101.       }
  102.       
  103.       public function parseImage(img:Bitmap, array:Array) : void
  104.       {
  105.       }
  106.       
  107.       private function changeGraphics(event:GraphicsChangedEvent) : void
  108.       {
  109.          var bm:Bitmap = new event.graph();
  110.          if(timer != null)
  111.          {
  112.             timer.removeEventListener("timer",changeFrame);
  113.             timer.stop();
  114.          }
  115.          initImage(bm,event.frameDelay,event.loop);
  116.          updateImage();
  117.       }
  118.       
  119.       private function initImage(bm:Bitmap, frameDelay:int, loop:Boolean = false) : void
  120.       {
  121.          h = bm.height;
  122.          w = bm.width;
  123.          this.loop = loop;
  124.          frames = 1;
  125.          frame = 0;
  126.          mainImage = bm.bitmapData;
  127.          if(timer != null)
  128.          {
  129.             timer.removeEventListener("timer",changeFrame);
  130.             timer.stop();
  131.          }
  132.          if(frameDelay > 0)
  133.          {
  134.             w = bm.height;
  135.             timer = new Timer(frameDelay);
  136.             timer.addEventListener("timer",changeFrame);
  137.             timer.start();
  138.             frames = bm.width / ┬ºgamegraphics:GraphicsObject┬º.h;
  139.          }
  140.       }
  141.       
  142.       public function destroy(event:Event) : void
  143.       {
  144.          obj.removeEventListener("objectMoved",move);
  145.          obj.removeEventListener("objectDestroyed",destroy);
  146.          if(timer != null)
  147.          {
  148.             timer.removeEventListener("timer",changeFrame);
  149.             timer.stop();
  150.          }
  151.          defaultImage.bitmapData.dispose();
  152.          mainImage.dispose();
  153.          GameGraphics.sprite.removeChild(image);
  154.       }
  155.       
  156.       public function updateImage() : void
  157.       {
  158.          image.bitmapData = new BitmapData(┬ºgamegraphics:GraphicsObject┬º.w,┬ºgamegraphics:GraphicsObject┬º.h);
  159.          image.bitmapData.copyPixels(mainImage,new Rectangle(frame * ┬ºgamegraphics:GraphicsObject┬º.w,0,┬ºgamegraphics:GraphicsObject┬º.w,┬ºgamegraphics:GraphicsObject┬º.h),new Point(0,0));
  160.       }
  161.    }
  162. }
  163.