SpineAnimationCtrl.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const {ccclass, property} = cc._decorator;
  2. @ccclass
  3. export default class SpineAnimationCtrl extends cc.Component {
  4. private spineNode: sp.Skeleton = null;
  5. private spineCallback: Function = null;
  6. private completeCallback: Function = null;
  7. protected onLoad(): void {
  8. this.initProp();
  9. }
  10. private initProp() {
  11. this.spineNode = this.node.getComponent(sp.Skeleton);
  12. this.spineNode.setCompleteListener((endEntry: any) => {
  13. this.spineCallback && this.spineCallback(endEntry);
  14. });
  15. }
  16. protected start(): void {
  17. }
  18. playAnimation(aniName: string = "animation", isLoop: boolean = false, playedStatus: boolean = false, aniName2: string = "", isLoop2: boolean = false) {
  19. this.node.active = true;
  20. this.spineNode.setToSetupPose();
  21. this.spineNode.setAnimation(0, aniName, isLoop);
  22. this.spineCallback = (endEntry: any) => {
  23. let isEnd = false;
  24. if (aniName == endEntry.animation.name) {
  25. if (this.isValidString(aniName2)) {
  26. return this.spineNode.setAnimation(0, aniName2, isLoop2);
  27. }
  28. isEnd = true;
  29. } else {
  30. isEnd = true;
  31. }
  32. if (isEnd) {
  33. this.completeCallback && this.completeCallback();
  34. if (isLoop || isLoop2) {
  35. return;
  36. }
  37. this.node.active = playedStatus;
  38. }
  39. }
  40. }
  41. setCompleteCallback(callback: Function) {
  42. this.completeCallback = callback;
  43. }
  44. private isValidString(str: string | null | undefined): boolean {
  45. return !!str && str.trim().length > 0;
  46. }
  47. // generateAllNodes () {
  48. // // 取得挂点工具
  49. // let attachUtil = this.spineNode.attachUtil;
  50. // attachUtil.generateAllAttachedNodes();
  51. // // 因为同名骨骼可能不止一个,所以需要返回数组
  52. // let boneNodes = attachUtil.getAttachedNodes(this.boneName);
  53. // // 取第一个骨骼作为挂点
  54. // let boneNode = boneNodes[0];
  55. // boneNode.addChild(cc.instantiate(this.targetPrefab));
  56. // }
  57. // destroyAllNodes () {
  58. // let attachUtil = this.spineNode.attachUtil;
  59. // attachUtil.destroyAllAttachedNodes();
  60. // }
  61. // protected onEnable(): void {
  62. // }
  63. // protected onDisable(): void {
  64. // }
  65. // protected onDestroy(): void {
  66. // }
  67. }