SpineEffectPlay.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Learn TypeScript:
  2. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
  10. import PoolMgr, { NODEPOOLPREFABTYPE } from "../../mgr/PoolMgr";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class SpineEffectPlay extends cc.Component {
  14. /**spine动画 */
  15. public spine: sp.Skeleton = null;
  16. /**当前的动画名称 */
  17. public ifLoop: boolean = false;
  18. /**当前的动画名称 */
  19. public curAniName: string = "";
  20. /**spine动画播放结束回调 */
  21. public spineAniEndCallBack: Function = null;
  22. @property({
  23. type: cc.Enum(NODEPOOLPREFABTYPE),
  24. serializable: true
  25. })
  26. public nodePoolPrefabType: NODEPOOLPREFABTYPE = 0;
  27. // LIFE-CYCLE CALLBACKS:
  28. // onLoad () {}
  29. start() {
  30. }
  31. onEnable() {
  32. this.spine = this.getComponent(sp.Skeleton);
  33. if (!this.spine) {
  34. mk.console.error("SpineEffectPlay 绑定的节点没有spine")
  35. return;
  36. }
  37. this.spine.setAnimation(0, this.spine.animation, false);
  38. this.spine.setCompleteListener(() => {
  39. this.spineLoopAniComplete();
  40. })
  41. }
  42. // update (dt) {}
  43. /**播放Spine动画 */
  44. playSpineAni(name: string, ifLoop: boolean = false, callBack: Function = null) {
  45. if (name) {
  46. this.curAniName = name;
  47. this.spine.setAnimation(0, name, ifLoop)
  48. }
  49. this.spineAniEndCallBack = callBack;
  50. }
  51. /**Spine动画循环播放结束回调函数 */
  52. spineLoopAniComplete() {
  53. //LogUtil.log("动画播放完毕!!!!!!")
  54. if (this.spineAniEndCallBack) {
  55. this.spineAniEndCallBack();
  56. }
  57. if (!this.ifLoop) {
  58. PoolMgr.Inst.recyclePoolPrefab(this.nodePoolPrefabType, this.node);
  59. }
  60. }
  61. clearLoopAniCompleteCallBack() {
  62. this.spineAniEndCallBack = null;
  63. }
  64. }