FrameAnimation.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /** @format */
  2. const {ccclass, property, executeInEditMode} = cc._decorator
  3. @ccclass('FrameConfig')
  4. class FrameConfig {
  5. @property(cc.Integer) frames: number = 0 //
  6. @property(cc.Integer) frameInterval = 1 // 帧数
  7. offsetFrame = 0 // 偏移量
  8. }
  9. @ccclass
  10. export default class FrameAnimation extends cc.Component {
  11. @property(cc.Sprite) sprite: cc.Sprite = null
  12. @property(cc.SpriteAtlas) spriteAtlas: cc.SpriteAtlas = null
  13. @property(FrameConfig) frameConfigs: FrameConfig[] = []
  14. @property(cc.Boolean) playOnLoad = false
  15. @property(cc.Boolean) loop = false
  16. @property(cc.Integer) defaultConfig = 0
  17. private _passInterval = 0
  18. private _currFrame = 0
  19. private _currFrameConfig: FrameConfig = null
  20. private _playing = false
  21. private _loop = true
  22. private _callback: Function
  23. start() {
  24. if (!this.sprite) this.sprite = this.getComponent(cc.Sprite)
  25. let offset = 0
  26. for (const config of this.frameConfigs) {
  27. config.offsetFrame += offset
  28. offset += config.frames
  29. }
  30. //在play处理,当play方法先执行,这里会产生覆盖false
  31. //this._loop = this.loop
  32. if (this.playOnLoad) {
  33. this._currFrameConfig = this.frameConfigs[this.defaultConfig]
  34. this._playing = true
  35. }
  36. }
  37. init(spriteAtlas, duration: number, loop: boolean) {
  38. this.spriteAtlas = spriteAtlas
  39. if (!this.sprite) {
  40. this.sprite = this.getComponent(cc.Sprite)
  41. }
  42. if (spriteAtlas && spriteAtlas.getSpriteFrames().length >= 0) {
  43. this.sprite.spriteFrame = spriteAtlas.getSpriteFrames()[0]
  44. }
  45. this._currFrameConfig = this.frameConfigs[this.defaultConfig]
  46. this._currFrameConfig.frames = this.spriteAtlas.getSpriteFrames().length
  47. if (!loop && duration && duration != Infinity) {
  48. this._currFrameConfig.frameInterval = Math.ceil(
  49. (duration * cc.game.getFrameRate()) / this._currFrameConfig.frames,
  50. )
  51. }
  52. }
  53. play(configIdx: number, loop: boolean, callback?: Function) {
  54. //this._currFrameConfig = this.frameConfigs[configIdx]
  55. this._loop = loop
  56. this._currFrame = 0
  57. this._playing = true
  58. this._callback = callback
  59. }
  60. stop() {
  61. this._playing = false
  62. }
  63. update(dt: number) {
  64. if (!this._playing) return
  65. this._passInterval++
  66. if (this._passInterval < this._currFrameConfig.frameInterval) return
  67. this._passInterval = 0
  68. this.sprite.spriteFrame = this.spriteAtlas.getSpriteFrames()[
  69. this._currFrameConfig.offsetFrame + this._currFrame
  70. ]
  71. this._currFrame++
  72. if (this._currFrame < this._currFrameConfig.frames) return
  73. if (this._loop) {
  74. this._currFrame = 0
  75. } else {
  76. this._playing = false
  77. this._callback && this._callback()
  78. }
  79. }
  80. }