Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. package ;
  2. import flash.display.Bitmap;
  3. import flash.display.BitmapData;
  4. import flash.display.Sprite;
  5. import flash.geom.Point;
  6. import flash.utils.Timer;
  7. import openfl.Assets;
  8. import flash.geom.Rectangle;
  9. import flash.events.TimerEvent;
  10.  
  11. /**
  12. * ...
  13. * @author Robin van Ee
  14. */
  15. class AnimSprite extends Sprite
  16. {
  17. var clip : Array<BitmapData>;
  18. var frame : Int = 0;
  19. var numFrames : Int;
  20. var timer : Timer;
  21.  
  22. public var origWidth : Float;
  23. public var origHeight : Float;
  24.  
  25. public function new(filename : String, frameWidth : Int, frameHeight : Int, nFrames : Int, ?fps : Int = 60)
  26. {
  27. super();
  28. var tileSheet = Assets.getBitmapData(filename);
  29.  
  30. numFrames = nFrames;
  31.  
  32. clip = new Array<BitmapData>();
  33.  
  34. var sheetWidth : Int = cast(tileSheet.width / frameWidth);
  35. var sheetHeight : Int = cast(tileSheet.height / frameHeight);
  36.  
  37. for (x in 0...sheetWidth)
  38. {
  39. for (y in 0...sheetHeight)
  40. {
  41. var tempRect : Rectangle = new Rectangle(x * frameWidth , y * frameHeight, frameWidth, frameHeight);
  42. var tempImage : BitmapData = new BitmapData(frameWidth, frameHeight, true, 0x00000000);
  43. tempImage.copyPixels(tileSheet, tempRect, new Point(0, 0));
  44. clip.push(tempImage);
  45. }
  46. }
  47.  
  48. graphics.beginBitmapFill(clip[frame]);
  49. graphics.drawRect(0, 0, clip[frame].width, clip[frame].height);
  50.  
  51. origWidth = width;
  52. origHeight = height;
  53.  
  54. timer = new Timer(1000 / fps, 0);
  55. timer.addEventListener (TimerEvent.TIMER, Render);
  56. }
  57.  
  58. private function Render(e:TimerEvent)
  59. {
  60. frame++;
  61. if (frame >= numFrames-1) frame = 0;
  62. graphics.clear();
  63. graphics.beginBitmapFill(clip[frame]);
  64. graphics.drawRect(0, 0, clip[frame].width, clip[frame].height);
  65. }
  66.  
  67. public function Play()
  68. {
  69. timer.start();
  70. }
  71.  
  72. public function Stop()
  73. {
  74. timer.stop();
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement