MovieClip.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { CCBoolean, CCFloat, CCInteger, Component, Rect, Size, Sprite, SpriteFrame, Texture2D, UITransform, Vec2, _decorator } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 动画播放器
  5. * @author
  6. *
  7. */
  8. @ccclass('MovieClip')
  9. export default class MovieClip extends Component {
  10. /** Sprite渲染器 */
  11. protected m_sprite: Sprite | null = null;;
  12. /** 动画计时间隔 每隔0.1s更新一帧 */
  13. protected timer: number = 0.1;
  14. /** 播放 时间 间隔 */
  15. @property({ type: CCFloat })
  16. public interval: number = 0.1;
  17. /** 贴图文件名 */
  18. @property({ type: Texture2D })
  19. public texture: Texture2D | null = null;
  20. /** 播放次数 */
  21. @property({ type: CCInteger })
  22. public playTimes: number = 0;
  23. @property({ type: CCInteger })
  24. public row: number = 4;
  25. /** 图片切割成几列 */
  26. @property({ type: CCInteger })
  27. public col: number = 4;
  28. @property({ type: CCInteger })
  29. public rowIndex: number = 0;
  30. @property(CCBoolean)
  31. public isAll: boolean = false;
  32. @property(CCBoolean)
  33. public autoPlayOnLoad: boolean = true;
  34. /** 播放完自动销毁 */
  35. @property(CCBoolean)
  36. public autoDestroy: boolean = false;
  37. @property(CCFloat)
  38. public begin: number = 0;
  39. @property(CCFloat)
  40. public end: number = 0;
  41. /** 动画帧数 */
  42. public totalFrame: number = 8;
  43. /** 当前帧数 */
  44. public currentFrame: number = 0;
  45. /** 当前播放了第几次 */
  46. private currentTimes: number = 0;
  47. /** 影片是否在跑动中 */
  48. public running: boolean = true;
  49. //private _direction:number = 1;
  50. private _playIndex: number = 0;
  51. private _pieceWidth: number = 0;
  52. private _pieceHeight: number = 0;
  53. private _bitmapArr: SpriteFrame[][] = [];
  54. onLoad() {
  55. //this. m_clips = new SpriteFrame[this.row][this.col];
  56. //Texture2D tex = Resources.Load<Texture2D>("Image/Avatar/" + m_sprite_name);
  57. //this.begin = 0;
  58. if (this.end == 0) {
  59. this.end = this.col;
  60. }
  61. this.rowIndex = this.clamp(this.rowIndex, 0, this.row - 1);
  62. this._pieceWidth = this.texture!.width / this.col;
  63. this._pieceHeight = this.texture!.height / this.row;
  64. this.m_sprite = this.getComponent(Sprite);
  65. if (!this.m_sprite) {
  66. this.m_sprite = this.addComponent(Sprite);
  67. }
  68. for (var i = 0; i < this.row; i++) {
  69. this._bitmapArr[i] = [];
  70. for (var j = 0; j < this.col; j++) {
  71. var spriteFrame: SpriteFrame = new SpriteFrame();
  72. spriteFrame.texture = this.texture!;
  73. spriteFrame.rect = new Rect(j * this._pieceWidth, i * this._pieceHeight, this._pieceWidth, this._pieceHeight);
  74. spriteFrame.rotated = false;
  75. spriteFrame.offset = new Vec2(0, 0);
  76. spriteFrame.originalSize = new Size(this._pieceWidth, this._pieceHeight);
  77. this._bitmapArr[i][j] = spriteFrame;
  78. }
  79. }
  80. this.m_sprite!.spriteFrame = this._bitmapArr[this.rowIndex][0];
  81. this.m_sprite!.spriteFrame.width
  82. var uiTransform = this.getComponent(UITransform);
  83. if (uiTransform) {
  84. uiTransform.width = this._pieceWidth;
  85. uiTransform.height = this._pieceHeight;
  86. }
  87. this.timer = 0;
  88. this.running = this.autoPlayOnLoad;
  89. }
  90. update(dt: number) {
  91. if (!this.running)
  92. return;
  93. if (this.playTimes != 0 && this.currentTimes == this.playTimes) {
  94. this.running = false;
  95. return;
  96. }
  97. this.timer -= dt;
  98. if (this.timer <= 0) {
  99. this.timer = this.interval;
  100. this.currentFrame = this.currentFrame % this.col;
  101. this.playAction();
  102. this.currentFrame++;
  103. if (this.currentFrame == this.col) {
  104. if (this.isAll) {
  105. this.rowIndex++;
  106. if (this.rowIndex == this.row) {
  107. this.currentTimes++;
  108. this.node.emit("completeTimes");
  109. if (this.playTimes != 0 && this.currentTimes == this.playTimes) {
  110. this.node.emit("complete");
  111. if (this.autoDestroy) {
  112. this.node.destroy();
  113. }
  114. }
  115. }
  116. this.rowIndex %= this.row;
  117. }
  118. else {
  119. this.currentTimes++;
  120. this.node.emit("completeTimes");
  121. if (this.playTimes != 0 && this.currentTimes == this.playTimes) {
  122. this.node.emit("complete");
  123. if (this.autoDestroy) {
  124. this.node.destroy();
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. private playAction() {
  132. this.rowIndex = this.clamp(this.rowIndex, 0, this.row - 1);
  133. this._playIndex = this._playIndex % (this.end - this.begin) + this.begin;
  134. this.m_sprite!.spriteFrame = this._bitmapArr[this.rowIndex][this._playIndex];
  135. //this.m_sprite.spriteFrame.setRect(this.rect);
  136. this._playIndex++;
  137. }
  138. /** 播放影片 */
  139. public play() {
  140. this.running = true;
  141. }
  142. /** 停止播放影片 */
  143. public stop() {
  144. this.running = false;
  145. }
  146. /**
  147. * 跳帧播放
  148. * @param frame 帧
  149. */
  150. public gotoAndPlay(frame: number) {
  151. this.running = true;
  152. this._playIndex = frame;
  153. this._playIndex = this.clamp(this._playIndex, 0, this.col - 1);
  154. }
  155. /**
  156. * 跳帧停止
  157. * @param frame 帧
  158. */
  159. public gotoAndStop(frame: number) {
  160. this.running = false;
  161. this._playIndex = frame;
  162. this._playIndex = this.clamp(this._playIndex, 0, this.col - 1);
  163. this.m_sprite!.spriteFrame = this._bitmapArr[this.rowIndex][this._playIndex];
  164. }
  165. public clamp(value: number, minLimit: number, maxLimit: number) {
  166. if (value < minLimit) {
  167. return minLimit;
  168. }
  169. if (value > maxLimit) {
  170. return maxLimit;
  171. }
  172. return value;
  173. }
  174. }