PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/AnimViewTool/src/fw/anim/AnimSprite.as

https://bitbucket.org/ntibor22/spritesheet
ActionScript | 251 lines | 157 code | 61 blank | 33 comment | 27 complexity | fd258269d77a558f5ce1e49d418051d1 MD5 | raw file
  1. package fw.anim
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. // ============================= Created by: Amos Laber, Dec 2, 2011
  6. //
  7. // AnimSprite is a class to diplay an animated sprite sheet
  8. // it is initialized with a sprite sheet and can hold multiple
  9. // animation sequnces that can be switched anytime
  10. //
  11. public class AnimSprite extends Bitmap
  12. {
  13. protected var mAnimSheet:AnimTextureSheet;
  14. protected var mSequences:Array;
  15. protected var curAnim:AnimSeqData; // current sequence
  16. protected var dirty:Boolean;
  17. protected var donePlaying:Boolean;
  18. protected var curIndex:uint; // Frame index into tile sheet
  19. protected var curFrame:uint; // Frame index in a sequence (local)
  20. protected var frameDelay:Number; // for fps
  21. // Internal, used to time each frame of animation.
  22. protected var frameTimer:Number;
  23. public static const LEFT:uint= 1;
  24. public static const RIGHT:uint= 2;
  25. public function AnimSprite(bitmapData:BitmapData=null)
  26. {
  27. super(bitmapData);
  28. frameTimer= 0;
  29. mSequences= [];
  30. }
  31. //
  32. // Initialize the sprite with the texture sheet.
  33. // supportFlip: set to true if you intend to use right/left flipping
  34. //
  35. public function initialize(sheet:AnimTextureSheet):void
  36. {
  37. if (!sheet) return;
  38. mAnimSheet= sheet;
  39. frameDelay=0;
  40. // Create the frame buffer
  41. bitmapData = new BitmapData(mAnimSheet.maxRect.width, mAnimSheet.maxRect.height);
  42. smoothing= true;
  43. curAnim = null;
  44. this.frame= 0;
  45. drawFrame(true);
  46. }
  47. private var _fps:int;
  48. public function get fps():int { return _fps; }
  49. public function forceFrameRate(fps:int):void
  50. {
  51. _fps = fps;
  52. frameDelay= 1.0 / fps;
  53. }
  54. public function destroy():void
  55. {
  56. if (bitmapData) {
  57. bitmapData.dispose();
  58. }
  59. }
  60. // Check if we are playing a sequence
  61. //
  62. public function isPlaying(index:int=0):Boolean
  63. {
  64. return !donePlaying;
  65. }
  66. public function get tileSheet():AnimTextureSheet { return mAnimSheet; }
  67. public function get numFrames():int { return mAnimSheet.numFrames; }
  68. public function get numSequences():int { return mSequences.length; }
  69. public function get seqFrame():uint { return curFrame; }
  70. public function get frame():uint { return curIndex; }
  71. public function set frame(val:uint):void
  72. {
  73. curFrame = val;
  74. if (curAnim)
  75. curIndex= curAnim.arFrames[curFrame];
  76. else curIndex= val;
  77. //curAnim = null;
  78. dirty = true;
  79. }
  80. public function getSequenceData(seq:int):AnimSeqData { return mSequences[seq]; }
  81. public function getSequence(seq:int):String { return mSequences[seq].seqName; }
  82. public function addSequence(name:String, frames:Array, frameRate:Number=0, looped:Boolean=true):void
  83. {
  84. mSequences.push(new AnimSeqData(name, frames, frameRate, looped));
  85. }
  86. public function findSequence(name:String):AnimSeqData
  87. {
  88. return findSequenceByName(name);
  89. }
  90. private function findSequenceByName(name:String):AnimSeqData
  91. {
  92. var aSeq:AnimSeqData;
  93. for (var i:int = 0; i < mSequences.length; i++) {
  94. aSeq = AnimSeqData(mSequences[i]);
  95. if (aSeq.seqName == name) {
  96. return aSeq;
  97. }
  98. }
  99. return null;
  100. }
  101. // Start playing a sequence
  102. //
  103. public function play(name:String=null):void
  104. {
  105. // Continue playing from last frame
  106. if (name == null) {
  107. donePlaying= false;
  108. dirty= true;
  109. frameTimer = 0;
  110. return;
  111. }
  112. curFrame = 0;
  113. curIndex = 0;
  114. frameTimer = 0;
  115. curAnim= findSequenceByName(name);
  116. if (!curAnim) {
  117. trace("play: cannot find sequence: " + name);
  118. return;
  119. }
  120. // trace("playing " + name +", frames: " + curAnim.arFrames.length);
  121. // Set to first frame
  122. curIndex= curAnim.arFrames[0];
  123. if (frameDelay==0) frameDelay= curAnim.delay;
  124. donePlaying= false;
  125. dirty= true;
  126. // Stop if we only have a single frame
  127. if (curAnim.arFrames.length==1) donePlaying= true;
  128. }
  129. // External use only (editor)
  130. //
  131. public function stop():void
  132. {
  133. donePlaying= true;
  134. }
  135. // Manually advance one frame forwards or back
  136. // Used by the viewer (not the game)
  137. //
  138. public function frameAdvance(next:Boolean):void
  139. {
  140. if (next) {
  141. if (curFrame < curAnim.arFrames.length -1) ++curFrame;
  142. }
  143. else {
  144. if (curFrame > 0) --curFrame;
  145. }
  146. curIndex= curAnim.arFrames[curFrame];
  147. dirty= true;
  148. }
  149. public function drawFrame(force:Boolean=false):void
  150. {
  151. if(force || dirty)
  152. drawFrameInternal();
  153. }
  154. // Call this function on every frame update
  155. //
  156. public function updateAnimation(delta:int):void
  157. {
  158. //trace("updateAnimation delta=" + delta + ", frd=" + frameDelay)
  159. if (curAnim!=null && frameDelay > 0 && !donePlaying) {
  160. // Check elapsed time and adjust to sequence rate
  161. frameTimer += (delta/1000.0);
  162. while(frameTimer > frameDelay) {
  163. frameTimer = frameTimer - frameDelay;
  164. advanceFrame();
  165. }
  166. }
  167. if (dirty) drawFrameInternal();
  168. }
  169. //
  170. //
  171. protected function advanceFrame():void
  172. {
  173. if (curFrame == curAnim.arFrames.length -1) {
  174. if (curAnim.loop)
  175. curFrame = 0;
  176. else donePlaying= true;
  177. }
  178. else ++curFrame;
  179. curIndex= curAnim.arFrames[curFrame];
  180. dirty= true;
  181. }
  182. // Internal function to update the current animation frame
  183. protected function drawFrameInternal():void
  184. {
  185. dirty = false;
  186. bitmapData.fillRect(bitmapData.rect, 0);
  187. mAnimSheet.drawFrame(curIndex, bitmapData);
  188. }
  189. }
  190. }