FrameAni.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('FrameAni')
  4. export class FrameAni extends Component {
  5. @property({type:[SpriteFrame]})
  6. ani1Sprites = []
  7. @property({type:[SpriteFrame]})
  8. ani2Sprites = []
  9. @property({type:[SpriteFrame]})
  10. ani3Sprites = []
  11. @property({type:[SpriteFrame]})
  12. ani4Sprites = []
  13. @property({type:[Number]})
  14. rates = []
  15. private rate:number=0;
  16. private curFrame:number = 0;
  17. private curTime:number=0;
  18. private aniSprite:Sprite;
  19. private isPlay:boolean = false;
  20. @property({type:Boolean})
  21. private isLoop:boolean = false;
  22. curAniSprites:SpriteFrame[];
  23. @property({type:Number})
  24. aniType:number = -1;
  25. private callback:Function;
  26. private self:any;
  27. protected onLoad(): void {
  28. this.SetAniSprites();
  29. this.aniSprite = this.node.getComponent(Sprite);
  30. console.log("aniType==>>"+this.aniType)
  31. if (this.aniType >= 0)
  32. {
  33. this.rate = this.rates[this.aniType];
  34. this.curFrame = 0;
  35. this.isPlay = true;
  36. this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
  37. }
  38. }
  39. public Stop()
  40. {
  41. this.isPlay = false;
  42. }
  43. private SetAniSprites()
  44. {
  45. if (this.aniType == 0)
  46. {
  47. this.curAniSprites= this.ani1Sprites;
  48. }else if (this.aniType == 1)
  49. {
  50. this.curAniSprites = this.ani2Sprites;
  51. }
  52. else if (this.aniType == 2)
  53. {
  54. this.curAniSprites = this.ani3Sprites;
  55. }
  56. else if (this.aniType == 3)
  57. {
  58. this.curAniSprites = this.ani4Sprites;
  59. }
  60. }
  61. public ChangeAniType( _aniType, _isLoop=false, isForce= false)
  62. {
  63. if (this.aniType == _aniType && !isForce) return;
  64. // Debug.Log("ChangeAniType==>>" + aniType + "," + _aniType + "," + _isLoop);
  65. this.aniType = _aniType;
  66. this.SetAniSprites();
  67. this.curFrame = 0;
  68. this.curTime=0;
  69. this.isPlay=true;
  70. this.isLoop = _isLoop;
  71. this.rate = this.rates[this.aniType];
  72. this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
  73. }
  74. public SetCallBack( _callback,_self)
  75. {
  76. this.callback = _callback;
  77. this.self = _self;
  78. }
  79. start() {
  80. }
  81. update(deltaTime: number) {
  82. if (!this.isPlay) return;
  83. this.curTime += deltaTime;
  84. // console.log("update==>>"+this.curTime+","+(1.0/ this.rate))
  85. if(this.curTime>=1.0/ this.rate)
  86. {
  87. this.curTime = 0;
  88. this.curFrame++;
  89. if (this.curFrame>= this.curAniSprites.length)
  90. {
  91. if (!this.isLoop)
  92. {
  93. this.isPlay = false;
  94. this.aniType = -1;
  95. if (this.callback!=null)
  96. this.callback(this.self);
  97. return;
  98. }
  99. else
  100. {
  101. this.curFrame = 0;
  102. }
  103. }
  104. this.aniSprite.spriteFrame = this.curAniSprites[this.curFrame];
  105. }
  106. }
  107. }