home *** CD-ROM | disk | FTP | other *** search
- package gamegraphics
- {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.utils.Timer;
- import game.GameObject;
- import game.GraphicsChangedEvent;
- import game.ObjectChangedEvent;
-
- public class GraphicsObject
- {
-
-
- private var sprite:Sprite;
-
- private var w:int;
-
- private var overWidth:int;
-
- private var defaultLoop:Boolean;
-
- private var image:Bitmap;
-
- private var timer:Timer;
-
- private var defaultImage:Bitmap;
-
- private var mainImage:BitmapData;
-
- private var loop:Boolean;
-
- private var frames:int = 1;
-
- private var h:int;
-
- private var overHeight:int;
-
- private var frame:int = 0;
-
- private var obj:GameObject;
-
- private var defaultFrameDelay:int;
-
- public function GraphicsObject(obj:GameObject, imageClass:Class, frameDelay:int = 0, loop:Boolean = false, overWidth:int = 0, overHeight:int = 0)
- {
- super();
- this.obj = obj;
- this.overWidth = overWidth;
- this.overHeight = overHeight;
- defaultImage = new imageClass();
- defaultFrameDelay = frameDelay;
- defaultLoop = loop;
- image = new Bitmap();
- initImage(defaultImage,frameDelay,loop);
- updateImage();
- refresh(null);
- GameGraphics.sprite.addChild(image);
- obj.addEventListener("objectChanged",refresh);
- obj.addEventListener("objectDestroyed",destroy);
- obj.addEventListener("graphicsChanged",changeGraphics);
- }
-
- public function move(event:ObjectChangedEvent) : void
- {
- image.x = event.x;
- image.y = event.y;
- }
-
- public function refresh(event:Event) : void
- {
- image.width = obj.w + overWidth;
- image.height = obj.h + overHeight;
- image.x = obj.x - overWidth / 2;
- image.y = obj.y - overHeight / 2;
- }
-
- private function changeFrame(event:Event) : void
- {
- ++frame;
- if(frame >= frames)
- {
- if(§gamegraphics:GraphicsObject§.loop == true)
- {
- frame = 0;
- }
- else
- {
- mainImage.dispose();
- mainImage = defaultImage.bitmapData;
- w = defaultImage.width;
- h = defaultImage.height;
- frames = §gamegraphics:GraphicsObject§.w / §gamegraphics:GraphicsObject§.h;
- initImage(defaultImage,defaultFrameDelay,defaultLoop);
- }
- }
- updateImage();
- }
-
- public function parseImage(img:Bitmap, array:Array) : void
- {
- }
-
- private function changeGraphics(event:GraphicsChangedEvent) : void
- {
- var bm:Bitmap = new event.graph();
- if(timer != null)
- {
- timer.removeEventListener("timer",changeFrame);
- timer.stop();
- }
- initImage(bm,event.frameDelay,event.loop);
- updateImage();
- }
-
- private function initImage(bm:Bitmap, frameDelay:int, loop:Boolean = false) : void
- {
- h = bm.height;
- w = bm.width;
- this.loop = loop;
- frames = 1;
- frame = 0;
- mainImage = bm.bitmapData;
- if(timer != null)
- {
- timer.removeEventListener("timer",changeFrame);
- timer.stop();
- }
- if(frameDelay > 0)
- {
- w = bm.height;
- timer = new Timer(frameDelay);
- timer.addEventListener("timer",changeFrame);
- timer.start();
- frames = bm.width / §gamegraphics:GraphicsObject§.h;
- }
- }
-
- public function destroy(event:Event) : void
- {
- obj.removeEventListener("objectMoved",move);
- obj.removeEventListener("objectDestroyed",destroy);
- if(timer != null)
- {
- timer.removeEventListener("timer",changeFrame);
- timer.stop();
- }
- defaultImage.bitmapData.dispose();
- mainImage.dispose();
- GameGraphics.sprite.removeChild(image);
- }
-
- public function updateImage() : void
- {
- image.bitmapData = new BitmapData(§gamegraphics:GraphicsObject§.w,§gamegraphics:GraphicsObject§.h);
- image.bitmapData.copyPixels(mainImage,new Rectangle(frame * §gamegraphics:GraphicsObject§.w,0,§gamegraphics:GraphicsObject§.w,§gamegraphics:GraphicsObject§.h),new Point(0,0));
- }
- }
- }
-