playerAnimation.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. const GameStatesStatic = require('GameStates');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. readyRunTag : 0,
  6. type : 0,
  7. player: {
  8. // ATTRIBUTES:
  9. default: null, // The default value will be used only when the component attaching
  10. // to a node for the first time
  11. type: cc.Node // optional, default is typeof default
  12. },
  13. skeletonDatas :{
  14. default: null, // The default value will be used only when the component attaching
  15. // to a node for the first time
  16. type: sp.SkeletonData // optional, default is typeof default
  17. }
  18. // bar: {
  19. // get () {
  20. // return this._bar;
  21. // },
  22. // set (value) {
  23. // this._bar = value;
  24. // }
  25. // },
  26. },
  27. // LIFE-CYCLE CALLBACKS:
  28. onLoad: function () {
  29. var spine = this.spine = this.getComponent('sp.Skeleton');
  30. // this.spine.skeletonData = this.skeletonDatas;
  31. // var s = this.spine.skeletonData.setSkin;
  32. // cc.log("运行的", this.spine.skeletonData.getRuntimeData().animation);
  33. // cc.loader.loadRes("boy/Boy_ai",function (err, res) {
  34. // cc.log("皮肤文件", err,res);
  35. // })
  36. // var realUrl = cc.url.raw("resources/boy/Boy_ai.png");
  37. // var texture = cc.textureCache.addImage(realUrl);
  38. // cc.log("皮肤文件", texture);
  39. // var realUrl = "333333";
  40. // // s.textures[0] = realUrl;
  41. // s.setTexture(realUrl);
  42. // this.spine.skeletonData = s;
  43. // cc.log("最新的文件", this.spine.getAttachment());
  44. cc.log("现在 播放 骨骼动画对象", spine);
  45. this.type = 0;
  46. this._hasStop = true;
  47. this.initData();
  48. this.idle();
  49. // this.spine.setAnimation(0, "Langan", false);
  50. // this.spine.timeScale = 0.0;
  51. // setTimeout(function () {
  52. // this.spine.setAnimation(0, "Langan", false);
  53. // this.spine.timeScale = 1.0;
  54. // }.bind(this),1000);
  55. // this.spine.paused = true;
  56. // this.paused();
  57. // .setStartListener()用来设置开始播放动画的事件监听。
  58. spine.setStartListener(function (trackEntry) {
  59. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  60. // cc.log("[track %s][animation %s] start.", trackEntry.trackIndex, animationName);
  61. // cc.log("现在 播放 的动画名字是", animationName);
  62. if (animationName == this.arr[this.type][this.walkLv]) {
  63. if (this.readyRunTag == 0) {
  64. cc.log("走着");
  65. this.player.getComponent("playerlinkagebg").readyRun(this.walkLv);
  66. }
  67. this.readyRunTag++;
  68. // this._hasStop = false;
  69. }
  70. this.setStatus(animationName);
  71. }.bind(this));
  72. // .setInterruptListener()用来设置动画被打断的事件监听。
  73. spine.setInterruptListener(function (trackEntry) {
  74. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  75. // cc.log("[track %s][animation %s] interrupt.", trackEntry.trackIndex, animationName);
  76. }.bind(this));
  77. // .setEndListener()用来设置动画播放完后的事件监听。
  78. spine.setEndListener(function (trackEntry) {
  79. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  80. // cc.log("[track %s][animation %s] end.", trackEntry.trackIndex, animationName);
  81. // cc.log("现在 停止 的动画名字是", animationName);
  82. if (animationName == this.statusArr[6] || animationName == this.statusArr[5]) {
  83. // this._hasStop = true;
  84. }
  85. }.bind(this));
  86. // .setDisposeListener()用来设置动画将被销毁的事件监听。
  87. spine.setDisposeListener(function (trackEntry) {
  88. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  89. // cc.log("[track %s][animation %s] will be disposed.", trackEntry.trackIndex, animationName);
  90. }.bind(this));
  91. // .setCompleteListener用来设置动画播放一次循环结束后的事件监听。
  92. spine.setCompleteListener(function (trackEntry, loopCount) {
  93. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  94. // if (animationName === 'attack') { //如果当前为射击状态,立马清除
  95. // // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  96. // this.spine.clearTrack(1);
  97. // }
  98. // cc.log("[track %s][animation %s] complete: %s", trackEntry.trackIndex, animationName, loopCount);
  99. }.bind(this));
  100. // .setEventListener()用来设置动画播放过程中帧事件的监听。
  101. spine.setEventListener(function (trackEntry, event) {
  102. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  103. // cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
  104. }.bind(this));
  105. // var self = this;
  106. // cc.eventManager.addListener({
  107. // event: cc.EventListener.TOUCH_ALL_AT_ONCE,
  108. // onTouchesBegan () {
  109. // self.toggleTimeScale();
  110. // }
  111. // }, this.node);
  112. },
  113. initData: function () {
  114. //初始化 动作列表
  115. this.statusArr = [
  116. //0
  117. "none",
  118. //1
  119. "idle",
  120. //2
  121. "idle-run",
  122. //3
  123. "run-01",
  124. //4
  125. "run-02",
  126. //5
  127. "run-03",
  128. //6
  129. "run-04",
  130. //7
  131. "run-05",
  132. //8
  133. "hurdling_1",
  134. //9
  135. "hurdling_2",
  136. //10
  137. "longjump-01",
  138. //11
  139. "longjump-02",
  140. "bybike_1",
  141. "bybike_2",
  142. "bybike_3",
  143. "bybike_4",
  144. "bybike_5",
  145. "bybike_5bike",
  146. "bybike_5boy",
  147. "bybike_redy",
  148. "bybike_redy_bike",
  149. "bybike_redy_boy",
  150. "Biaoqiang01",
  151. "Biaoqiang02",
  152. "Biaoqiang03",
  153. "javelin_1",
  154. "javelin_2",
  155. "javelin_2a",
  156. "javelin_3",
  157. "javelin_3a",
  158. "javelin_4",
  159. "javelin_5",
  160. "javelin_redy"
  161. ];
  162. //todo 准备跑步动作
  163. this.statusArr_redyrun = [
  164. //0
  165. "idle",
  166. //1
  167. "idle-run",
  168. ];
  169. this.arr = [];
  170. //todo 跑步动作
  171. this.statusArr_run = [
  172. //0
  173. "run-01",
  174. //1
  175. "run-02",
  176. //2
  177. "run-03",
  178. //3
  179. "run-05",
  180. //4
  181. "run-04",
  182. ];
  183. this.arr.push(this.statusArr_run);
  184. //todo 跨栏
  185. this.statusArr_hurdling = [
  186. //0
  187. "hurdling_1",
  188. //1
  189. "hurdling_2",
  190. ];
  191. //todo 跳远
  192. this.statusArr_longjump = [
  193. //0
  194. "longjump-01",
  195. //1
  196. "longjump-02",
  197. ];
  198. //todo 蹬三轮
  199. this.statusArr_bike = [
  200. //0
  201. "bybike_1",
  202. //1
  203. "bybike_2",
  204. //2
  205. "bybike_3",
  206. //3
  207. "bybike_4",
  208. //4
  209. "bybike_5",
  210. //5
  211. "bybike_5bike",
  212. //6
  213. "bybike_5boy",
  214. //7
  215. "bybike_redy",
  216. //8
  217. "bybike_redy_bike",
  218. //9
  219. "bybike_redy_boy",
  220. ];
  221. this.arr.push(this.statusArr_bike);
  222. //todo 标枪项目
  223. this.statusArr_javelin = [
  224. //0
  225. "javelin_1",
  226. //1
  227. "javelin_2",
  228. //2
  229. "javelin_2a",
  230. //3
  231. "javelin_3",
  232. //4
  233. "javelin_3a",
  234. //5
  235. "javelin_4",
  236. //6
  237. "javelin_5",
  238. //7
  239. "javelin_redy"
  240. ];
  241. this.arr.push(this.statusArr_javelin);
  242. //todo 标枪
  243. this.statusArr_biaoqiang = [
  244. "Biaoqiang01",
  245. "Biaoqiang02",
  246. "Biaoqiang03",
  247. ];
  248. //初始化动作状态
  249. this.setStatus(this.statusArr[1]);
  250. //初始化跳跃状态
  251. // this.setJumpTag(0);
  252. //设置当前没有 任何加速 机会
  253. this.setMyTag(null);
  254. },
  255. setMyTag: function(obj) {
  256. this.myTag = obj;
  257. },
  258. idle : function () {
  259. this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
  260. },
  261. walk: function () {
  262. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  263. this.spine.setAnimation(0, this.statusArr[3], true);
  264. },
  265. readyRun: function () {
  266. this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
  267. this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
  268. this.spine.addAnimation(0, this.statusArr_run[0], true);
  269. //奔跑等级
  270. this.walkLv = 0;
  271. },
  272. speedUp: function (type,lv) {
  273. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  274. //不传就是加1
  275. if (lv == null) {
  276. if (this.walkLv < 2) {
  277. this.walkLv++;
  278. }
  279. } else {
  280. if (this.walkLv < 2) {
  281. this.walkLv = lv;
  282. }
  283. }
  284. // switch (){
  285. // case :
  286. //
  287. // break;
  288. // }
  289. cc.log("动画是的很么",this.arr[0][this.walkLv]);
  290. this.spine.addAnimation(0, this.arr[this.type][this.walkLv], true);
  291. },
  292. speedDown: function (lv) {
  293. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  294. //不传就是加1
  295. if (lv == null) {
  296. if (this.walkLv > 0) {
  297. this.walkLv--;
  298. }
  299. } else {
  300. this.walkLv = lv;
  301. }
  302. this.spine.addAnimation(0, this.arr[this.type][this.walkLv], true);
  303. },
  304. stagger: function () {
  305. cc.log("帅哦到了",this.arr[this.type][3]);
  306. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  307. this.spine.setAnimation(0, this.arr[this.type][3], false);
  308. this.speedDown();
  309. },
  310. longjump : function () {
  311. this.spine.setAnimation(0, this.statusArr_longjump[0], false);
  312. this.spine.addAnimation(0, this.statusArr_run[this.walkLv], true);
  313. // this.speedUp();
  314. },
  315. longjump_stagger : function () {
  316. this.spine.setAnimation(0, this.statusArr_longjump[1], false);
  317. this.speedDown();
  318. },
  319. hurdling : function () {
  320. this.spine.setAnimation(0, this.statusArr_hurdling[0], false);
  321. this.spine.addAnimation(0, this.statusArr_run[this.walkLv], true);
  322. },
  323. hurdling_stagger : function () {
  324. this.spine.setAnimation(0, this.statusArr_hurdling[1], false);
  325. this.speedDown();
  326. },
  327. //单纯的停止
  328. stop: function () {
  329. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  330. this.spine.clearTrack(0);
  331. this.setStatus(this.statusArr_redyrun[0]);
  332. },
  333. //调用一次 暂停 再调用 恢复
  334. paused: function () {
  335. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  336. if (this.spine.paused) {
  337. this.spine.paused = false;
  338. } else {
  339. this.spine.paused = true;
  340. }
  341. },
  342. /**
  343. * 设置当前的运动状态
  344. * @param status
  345. */
  346. setStatus: function (status) {
  347. this.myStatus = status;
  348. cc.log("设置当前播放动画是",status);
  349. this.player.getComponent("playerlinkagebg").upDatawalkLv(this.walkLv);
  350. },
  351. //按屏幕了
  352. setOnTouchListener: function () {
  353. // this.jump();
  354. // this.toggleTimeScale();
  355. // cc.log("什么名字",this.spine.animation);
  356. // this.type = 1;
  357. if (this.spine.animation ==this.statusArr_redyrun[0] ) {
  358. switch (this.type){
  359. case 0 :
  360. this.readyRun();
  361. break;
  362. case 1 :
  363. this.upBike();
  364. break;
  365. }
  366. return;
  367. }
  368. // this.longjump();
  369. // return;
  370. if (this._hasStop) {
  371. this.speedUp();
  372. // this.paused();
  373. }
  374. // cc.log("点击了啊");
  375. },
  376. setSkeletonDataJson : function (num) {
  377. // this.spine.skeletonData = this.skeletonDatas;
  378. },
  379. //todo 跨栏 跳跃
  380. setOnHurdlingListener: function () {
  381. // if (this._hasStop) {
  382. // this.speedUp();
  383. // }
  384. this.hurdling();
  385. },
  386. //todo 跨栏 踉跄
  387. setOnHurdlingStaggerListener: function () {
  388. this.hurdling_stagger();
  389. },
  390. //todo 跳远跳跃
  391. setOnLongJumpListener: function () {
  392. // if (this._hasStop) {
  393. // this.speedUp();
  394. // }
  395. this.longjump();
  396. },
  397. //todo 跳远 踉跄
  398. setOnLongJumpStaggerListener: function () {
  399. this.longjump_stagger();
  400. },
  401. //摔倒了
  402. setOnTripListener: function () {
  403. // this.jump();
  404. // this.toggleTimeScale();
  405. this.stagger();
  406. // this.speedDown();
  407. },
  408. toggleTimeScale: function () {
  409. // .timeScale当前骨骼中所有动画的时间缩放率。
  410. if (this.spine.timeScale === 1.0) {
  411. this.spine.timeScale = 0.3;
  412. } else {
  413. this.spine.timeScale = 1.0;
  414. }
  415. },
  416. upBike : function () {
  417. this.spine.setAnimation(0, this.statusArr_run[0], false);
  418. this.spine.addAnimation(0, this.statusArr_bike[9], false);
  419. this.spine.addAnimation(0, this.statusArr_bike[0], true);
  420. // this.setStatus(this.statusArr[this.walkLv]);
  421. this.walkLv = 0;
  422. },
  423. // start () {
  424. //
  425. // },
  426. // update (dt) {},
  427. });