Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.geom.Point;
- import flash.utils.Timer;
- import openfl.Assets;
- import flash.geom.Rectangle;
- import flash.events.TimerEvent;
- /**
- * ...
- * @author Robin van Ee
- */
- class AnimSprite extends Sprite
- {
- var clip : Array<BitmapData>;
- var frame : Int = 0;
- var numFrames : Int;
- var timer : Timer;
- public var origWidth : Float;
- public var origHeight : Float;
- public function new(filename : String, frameWidth : Int, frameHeight : Int, nFrames : Int, ?fps : Int = 60)
- {
- super();
- var tileSheet = Assets.getBitmapData(filename);
- numFrames = nFrames;
- clip = new Array<BitmapData>();
- var sheetWidth : Int = cast(tileSheet.width / frameWidth);
- var sheetHeight : Int = cast(tileSheet.height / frameHeight);
- for (x in 0...sheetWidth)
- {
- for (y in 0...sheetHeight)
- {
- var tempRect : Rectangle = new Rectangle(x * frameWidth , y * frameHeight, frameWidth, frameHeight);
- var tempImage : BitmapData = new BitmapData(frameWidth, frameHeight, true, 0x00000000);
- tempImage.copyPixels(tileSheet, tempRect, new Point(0, 0));
- clip.push(tempImage);
- }
- }
- graphics.beginBitmapFill(clip[frame]);
- graphics.drawRect(0, 0, clip[frame].width, clip[frame].height);
- origWidth = width;
- origHeight = height;
- timer = new Timer(1000 / fps, 0);
- timer.addEventListener (TimerEvent.TIMER, Render);
- }
- private function Render(e:TimerEvent)
- {
- frame++;
- if (frame >= numFrames-1) frame = 0;
- graphics.clear();
- graphics.beginBitmapFill(clip[frame]);
- graphics.drawRect(0, 0, clip[frame].width, clip[frame].height);
- }
- public function Play()
- {
- timer.start();
- }
- public function Stop()
- {
- timer.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement