SpineTest.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. export default class NewClass extends cc.Component {
  4. @property(sp.Skeleton)
  5. spines: sp.Skeleton[] = [];
  6. index: number = 0;
  7. aniNames: string[];
  8. aniIndex: number = 0;
  9. curSpineNode: cc.Node = null;
  10. onLoad() {
  11. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  12. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyUp, this);
  13. }
  14. onKeyDown(event: cc.Event.EventKeyboard) {
  15. switch (event.keyCode) {
  16. case cc.macro.KEY.right:
  17. //选择人物
  18. if (this.spines.length - 1 > this.index)
  19. this.index++
  20. else
  21. this.index = 0;
  22. this.aniIndex = 0;
  23. console.log("spine role:" + this.spines[this.index].name);
  24. this.aniNames = [];
  25. let anis = this.spines[this.index].skeletonData.skeletonJson.animations;
  26. for (let i in anis) {
  27. this.aniNames.push(i);
  28. //console.log("Spine:" + i);
  29. }
  30. if (this.curSpineNode) {
  31. this.curSpineNode.scale = 1;
  32. }
  33. this.curSpineNode = this.spines[this.index].node;
  34. this.curSpineNode.scale = 1.2;
  35. break;
  36. case cc.macro.KEY.left:
  37. if (this.index > 0)
  38. this.index--
  39. else
  40. this.index = this.spines.length - 1;
  41. this.aniIndex = 0;
  42. this.aniNames = [];
  43. console.log("spine role:" + this.spines[this.index].name);
  44. let anis2 = this.spines[this.index].skeletonData.skeletonJson.animations;
  45. for (let i in anis2) {
  46. this.aniNames.push(i);
  47. //console.log("Spine:" + i);
  48. }
  49. if (this.curSpineNode) {
  50. this.curSpineNode.scale = 1;
  51. }
  52. this.curSpineNode = this.spines[this.index].node;
  53. this.curSpineNode.scale = 1.2;
  54. break;
  55. case cc.macro.KEY.up:
  56. if (this.aniNames.length - 1 > this.aniIndex)
  57. this.aniIndex++;
  58. else
  59. this.aniIndex = 0;
  60. break;
  61. case cc.macro.KEY.down:
  62. if (this.aniIndex > 0)
  63. this.aniIndex--
  64. else
  65. this.aniIndex = this.aniNames.length - 1;
  66. break;
  67. case cc.macro.KEY.space:
  68. if (this.aniNames != null) {
  69. console.log("ani name:" + this.aniNames[this.aniIndex]);
  70. //this.spines[this.index].clearTrack(0);
  71. this.spines[this.index].clearTracks();
  72. //this.spines[this.index].invalidAnimationCache();
  73. this.spines[this.index].setToSetupPose();
  74. this.spines[this.index].setCompleteListener(()=>{
  75. console.log("ani:"+this.aniNames[this.aniIndex]+"End");
  76. })
  77. this.spines[this.index].setAnimation(0, this.aniNames[this.aniIndex], false);
  78. console.log("Ani Duration:"+this.spines[this.index].findAnimation(this.aniNames[this.aniIndex]).duration)
  79. } else {
  80. console.log("no spine names");
  81. }
  82. break;
  83. }
  84. }
  85. onKeyUp(event: cc.Event.EventKeyboard) {
  86. }
  87. onDestroy() {
  88. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  89. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
  90. }
  91. }
  92. //{
  93. // "obj": 0,
  94. // "evtId": 1,
  95. // "delay": 0,
  96. // "posx0": 185,
  97. // "posy0": -170,
  98. // "posx1": 185,
  99. // "posy1": -521,
  100. // "posDuration": 1,
  101. // "sameAct": [
  102. // {
  103. // "obj": 0,
  104. // "evtId": 4,
  105. // "delay": 0,
  106. // "ani": "run_diaoluo",
  107. // "aniDuration": 0,
  108. // "aniLoop": 0,
  109. // "sameAct": [
  110. // {
  111. // "obj": 0,
  112. // "evtId": 5,
  113. // "delay": 0.5,
  114. // "clip": "playDown_clip",
  115. // "volume": 1,
  116. // "desTime": 0,
  117. // "sameAct": []
  118. // }
  119. // ]
  120. // }
  121. // ]
  122. //}