Charactor_javeLin.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. const GameStatesStatic = require('GameStates');
  2. const PlayerState = require('PlayerState');
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. readyRunTag : 0,
  7. //当前进行的项目
  8. type : 0,
  9. Canvas: {
  10. default: null,
  11. type: cc.Node
  12. },
  13. },
  14. onLoad: function () {
  15. // var self = this;
  16. // cc.eventManager.addListener({
  17. // event: cc.EventListener.TOUCH_ALL_AT_ONCE,
  18. // onTouchesBegan () {
  19. // self.toggleTimeScale();
  20. // }
  21. // }, this.node);
  22. },
  23. start : function () {
  24. var spine = this.spine = this.getComponent('sp.Skeleton');
  25. // cc.log("现在 播放 骨骼动画对象", spine);
  26. this._hasStop = true;
  27. // this.setType();
  28. this.initData();
  29. // this.getSpeedLv();
  30. // cc.log("当前游戏", this.Canvas.getComponent("GameStates").CurrentProgress);
  31. // this.javelin();
  32. // this.spine.setAnimation(0, "langan", true);
  33. // this.walk();
  34. // .setStartListener()用来设置开始播放动画的事件监听。
  35. },
  36. setListener : function () {
  37. var spine = this.spine = this.getComponent('sp.Skeleton');
  38. spine.setStartListener(function (trackEntry) {
  39. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  40. // cc.log("[track %s][animation %s] start.", trackEntry.trackIndex, animationName);
  41. // cc.log("现在 播放 的动画名字是", animationName);
  42. if (animationName == this.arr[this.type][this.walkLv]) {
  43. if (this.readyRunTag == 0) {
  44. // cc.log("走着");
  45. // this.player.getComponent("playerlinkagebg").readyRun(this.walkLv);
  46. }
  47. this.readyRunTag++;
  48. // this._hasStop = false;
  49. }
  50. this.setStatus(animationName);
  51. }.bind(this));
  52. // .setInterruptListener()用来设置动画被打断的事件监听。
  53. spine.setInterruptListener(function (trackEntry) {
  54. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  55. // cc.log("[track %s][animation %s] interrupt.", trackEntry.trackIndex, animationName);
  56. if (this.interruptListener!=null) {
  57. this.interruptListener(animationName);
  58. // cc.log("现在 打断 的动画名字是", animationName);
  59. // this.spine.timeScale = 1;
  60. }
  61. }.bind(this));
  62. // .setEndListener()用来设置动画播放完后的事件监听。
  63. spine.setEndListener(function (trackEntry) {
  64. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  65. // cc.log("[track %s][animation %s] end.", trackEntry.trackIndex, animationName);
  66. // cc.log("现在 停止 的动画名字是", animationName);
  67. if (animationName == this.statusArr[6] || animationName == this.statusArr[5]) {
  68. // this._hasStop = true;
  69. }
  70. if (this.endListener!=null) {
  71. this.endListener(animationName);
  72. // this.spine.timeScale = 1;
  73. }
  74. }.bind(this));
  75. // .setDisposeListener()用来设置动画将被销毁的事件监听。
  76. spine.setDisposeListener(function (trackEntry) {
  77. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  78. // cc.log("[track %s][animation %s] will be disposed.", trackEntry.trackIndex, animationName);
  79. }.bind(this));
  80. // .setCompleteListener用来设置动画播放一次循环结束后的事件监听。
  81. spine.setCompleteListener(function (trackEntry, loopCount) {
  82. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  83. // if (animationName === 'attack') { //如果当前为射击状态,立马清除
  84. // // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  85. // this.spine.clearTrack(1);
  86. // }
  87. // cc.log("[track %s][animation %s] complete: %s", trackEntry.trackIndex, animationName, loopCount);
  88. }.bind(this));
  89. // .setEventListener()用来设置动画播放过程中帧事件的监听。
  90. spine.setEventListener(function (trackEntry, event) {
  91. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  92. // cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
  93. }.bind(this));
  94. },
  95. initData: function () {
  96. //初始化 动作列表
  97. this.statusArr = [
  98. //0
  99. "none",
  100. //1
  101. "idle",
  102. //2
  103. "idle-run",
  104. //3
  105. "run-01",
  106. //4
  107. "run-02",
  108. //5
  109. "run-03",
  110. //6
  111. "run-04",
  112. //7
  113. "run-05",
  114. //8
  115. "hurdling_1",
  116. //9
  117. "hurdling_2",
  118. //10
  119. "longjump-01",
  120. //11
  121. "longjump-02",
  122. "bybike_1",
  123. "bybike_2",
  124. "bybike_3",
  125. "bybike_4",
  126. "bybike_5",
  127. "bybike_5bike",
  128. "bybike_5boy",
  129. "bybike_redy",
  130. "bybike_redy_bike",
  131. "bybike_redy_boy",
  132. "Biaoqiang01",
  133. "Biaoqiang02",
  134. "Biaoqiang03",
  135. "javelin_1",
  136. "javelin_2",
  137. "javelin_2a",
  138. "javelin_3",
  139. "javelin_3a",
  140. "javelin_4",
  141. "javelin_5",
  142. "javelin_redy"
  143. ];
  144. //todo 准备跑步动作
  145. this.statusArr_redyrun = [
  146. //0
  147. "idle",
  148. //1
  149. "idle-run",
  150. ];
  151. // Race:0,
  152. // Hurdle:1,
  153. // Javelin:2,
  154. // LongJump:3,
  155. // RideBike:4,
  156. this.arr = [];
  157. //todo 跑步动作
  158. this.statusArr_run = [
  159. //0
  160. "run-01",
  161. //1
  162. "run-02",
  163. //2
  164. "run-03",
  165. //3
  166. "run-05",
  167. //4
  168. "run-04",
  169. ];
  170. this.arr.push(this.statusArr_run);
  171. //todo 跳远
  172. var a = [];
  173. this.statusArr_longjump = a.concat(this.statusArr_run);
  174. //5
  175. this.statusArr_longjump.push("longjump-01");
  176. //6
  177. this.statusArr_longjump.push("longjump-02");
  178. this.arr.push(this.statusArr_longjump);
  179. //todo 跨栏
  180. a = [];
  181. this.statusArr_hurdling = a.concat(this.statusArr_run);
  182. //5
  183. this.statusArr_hurdling.push("hurdling_1");
  184. //6
  185. this.statusArr_hurdling.push("hurdling_2");
  186. this.arr.push(this.statusArr_hurdling);
  187. //todo 蹬三轮
  188. this.statusArr_bike = [
  189. //0
  190. "bybike_1",
  191. //1
  192. "bybike_2",
  193. //2
  194. "bybike_3",
  195. //3
  196. "bybike_4",
  197. //4
  198. "bybike_5",
  199. //5
  200. "bybike_5bike",
  201. //6
  202. "bybike_5boy",
  203. //7
  204. "bybike_redy",
  205. //8
  206. "bybike_redy_bike",
  207. //9
  208. "bybike_redy_boy",
  209. ];
  210. this.arr.push(this.statusArr_bike);
  211. //todo 标枪项目
  212. this.statusArr_javelin = [
  213. //0
  214. "javelin_1",
  215. //1
  216. "javelin_2",
  217. //2
  218. "javelin_2a",
  219. //3
  220. "javelin_3",
  221. //4
  222. "javelin_3a",
  223. //5
  224. "javelin_4",
  225. //6
  226. "javelin_5",
  227. //7
  228. "javelin_redy"
  229. ];
  230. this.arr.push(this.statusArr_javelin);
  231. // cc.log("所有动作", this.arr);
  232. //todo 标枪
  233. this.statusArr_biaoqiang = [
  234. "Biaoqiang01",
  235. "Biaoqiang02",
  236. "Biaoqiang03",
  237. ];
  238. //初始化动作状态
  239. this.setStatus(this.statusArr[1]);
  240. //初始化跳跃状态
  241. // this.setJumpTag(0);
  242. //设置当前没有 任何加速 机会
  243. this.setMyTag(null);
  244. },
  245. setMyTag: function (obj) {
  246. this.myTag = obj;
  247. },
  248. idle : function () {
  249. this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
  250. },
  251. walk: function () {
  252. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  253. this.spine.setAnimation(0, this.statusArr[3], true);
  254. },
  255. readyRun: function () {
  256. // this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
  257. // this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
  258. this.spine.setAnimation(0, this.statusArr_run[0], true);
  259. //奔跑等级
  260. this.walkLv = 0;
  261. },
  262. stagger: function () {
  263. // cc.log("帅哦到了",this.arr[this.type][3]);
  264. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  265. this.spine.setAnimation(0, this.arr[this.type][3], false);
  266. // this.setCurrentSpeed();
  267. },
  268. longjump : function () {
  269. // this.spine.timeScale = 1.5;
  270. this.spine.setAnimation(0, this.statusArr_longjump[5], false);
  271. setTimeout(function () {
  272. this.spine.setAnimation(0, this.statusArr_longjump[6], false);
  273. if (this.timeOutLongJumpListener!=null) {
  274. this.timeOutLongJumpListener();
  275. }
  276. // this.spine.addAnimation(0, this.statusArr_longjump[this.walkLv], true,0.5);
  277. }.bind(this),1000);
  278. setTimeout(function () {
  279. this.paused();
  280. }.bind(this),1500);
  281. setTimeout(function () {
  282. this.paused();
  283. // this.setCurrentSpeed();
  284. }.bind(this),2500);
  285. },
  286. timeOutLongJumpListener : function () {
  287. },
  288. setTimeOutLongJumpListener : function (timeOutLongJumpListener) {
  289. this.timeOutLongJumpListener = timeOutLongJumpListener;
  290. },
  291. longjump_stagger : function () {
  292. this.spine.setAnimation(0, this.statusArr_longjump[1], false);
  293. // this.setCurrentSpeed();
  294. },
  295. hurdling : function () {
  296. this.spine.setAnimation(0, this.statusArr_hurdling[5], false);
  297. this.spine.addAnimation(0, this.statusArr_hurdling[this.walkLv], true);
  298. // this.setCurrentSpeed();
  299. },
  300. hurdling_stagger : function () {
  301. this.spine.setAnimation(0, this.statusArr_hurdling[1], false);
  302. // this.setCurrentSpeed();
  303. },
  304. //单纯的停止
  305. stop: function () {
  306. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  307. this.spine.clearTrack(0);
  308. this.setStatus(this.statusArr_redyrun[0]);
  309. },
  310. //调用一次 暂停 再调用 恢复
  311. paused: function () {
  312. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  313. if (this.spine.paused) {
  314. this.spine.paused = false;
  315. } else {
  316. this.spine.paused = true;
  317. }
  318. },
  319. /**
  320. * 设置当前的运动状态
  321. * @param status
  322. */
  323. setStatus: function (status) {
  324. this.myStatus = status;
  325. // cc.log("设置当前播放动画是",status);
  326. // this.player.getComponent("playerlinkagebg").upDatawalkLv(this.walkLv);
  327. },
  328. //按屏幕了
  329. setOnTouchListener: function () {
  330. // this.jump();
  331. // this.toggleTimeScale();
  332. // cc.log("什么名字",this.spine.animation);
  333. // cc.log("当前游戏 按屏幕了", this.Canvas.getComponent("GameStates").CurrentProgress);
  334. if (this.spine.animation ==this.statusArr_redyrun[0] ) {
  335. switch (this.type){
  336. case 0 :
  337. this.readyRun();
  338. break;
  339. case 1 :
  340. this.upBike();
  341. break;
  342. }
  343. return;
  344. }
  345. this.setType();
  346. if (this._hasStop) {
  347. // this.setCurrentSpeed();
  348. }
  349. },
  350. //todo 跨栏 跳跃
  351. setOnHurdlingListener: function () {
  352. this.setType();
  353. // if (this._hasStop) {
  354. // this.speedUp();
  355. // }
  356. this.hurdling();
  357. },
  358. //todo 跨栏 踉跄
  359. setOnHurdlingStaggerListener: function () {
  360. this.setType();
  361. this.hurdling_stagger();
  362. },
  363. //todo 跳远跳跃
  364. setOnLongJumpListener: function () {
  365. this.setType();
  366. // if (this._hasStop) {
  367. // this.speedUp();
  368. // }
  369. this.longjump();
  370. },
  371. //todo 跳远 踉跄
  372. setOnLongJumpStaggerListener: function () {
  373. this.setType();
  374. this.longjump_stagger();
  375. },
  376. //摔倒了
  377. setOnTripListener: function () {
  378. this.setType();
  379. // this.jump();
  380. // this.toggleTimeScale();
  381. this.stagger();
  382. // this.speedDown();
  383. //
  384. // this.upBike();
  385. },
  386. toggleTimeScale: function () {
  387. // .timeScale当前骨骼中所有动画的时间缩放率。
  388. if (this.spine.timeScale === 1.0) {
  389. this.spine.timeScale = 0.3;
  390. } else {
  391. this.spine.timeScale = 1.0;
  392. }
  393. },
  394. setEndListener : function (endListener) {
  395. this.endListener = endListener
  396. },
  397. endListener : function () {
  398. },
  399. setInterruptListener : function (interruptListener) {
  400. this.interruptListener = interruptListener
  401. },
  402. interruptListener : function () {
  403. },
  404. //todo 快上车 没时间解释了
  405. upBike : function () {
  406. this.setType();
  407. // this.spine.setAnimation(0, this.statusArr_run[0], false);
  408. this.spine.setAnimation(0, this.statusArr_bike[9], false);
  409. this.spine.addAnimation(0, this.statusArr_bike[0], true);
  410. // this.setStatus(this.statusArr[this.walkLv]);
  411. this.walkLv = 0;
  412. },
  413. //todo 快拿标枪 没时间解释了
  414. upJavelin : function () {
  415. this.setType();
  416. // this.spine.setAnimation(0, this.statusArr_run[0], false);
  417. this.spine.setAnimation(0, this.statusArr_javelin[0], true);
  418. this.walkLv = 0;
  419. // this.setStatus(this.statusArr[this.walkLv]);
  420. // this.setCurrentSpeed();
  421. },
  422. //todo 快扔标枪 没时间解释了
  423. upJavelin_go : function () {
  424. this.setType();
  425. // this.spine.setAnimation(0, this.statusArr_run[0], false);
  426. this.spine.setAnimation(0, this.statusArr_javelin[6], false);
  427. this.walkLv = 0;
  428. // this.setStatus(this.statusArr[this.walkLv]);
  429. // this.setCurrentSpeed();
  430. },
  431. //todo 标枪 没时间解释了
  432. javelin : function (data,starListener,endListener) {
  433. var spine = this.spine = this.getComponent('sp.Skeleton');
  434. this.initData();
  435. this.setListener();
  436. var actionBy = cc.jumpBy(data.duration, data.position, data.y, data.height, data.jumps);
  437. var start = cc.callFunc(function () {
  438. if (starListener!=null) {
  439. starListener();
  440. }
  441. }.bind(this), this);
  442. var stop = cc.callFunc(function () {
  443. if (endListener!=null) {
  444. endListener();
  445. }
  446. }.bind(this), this);
  447. var myAction = cc.sequence(start,actionBy,stop);
  448. this.node.runAction(myAction);
  449. // var anim = this.getComponent(cc.Animation);
  450. // cc.log("要播放动画",anim);
  451. // anim.play();
  452. // this.setType();
  453. // this.spine.setAnimation(0, this.statusArr_run[0], false);
  454. this.spine.setAnimation(0, this.statusArr_biaoqiang[2], false);
  455. // this.spine.addAnimation(0, this.statusArr_biaoqiang[1], false);
  456. // this.spine.addAnimation(0, this.statusArr_biaoqiang[2], false);
  457. this.walkLv = 0;
  458. // this.setStatus(this.statusArr[this.walkLv]);
  459. // this.setCurrentSpeed();
  460. },
  461. setType : function () {
  462. var temp = this.Canvas.getComponent("GameStates").CurrentProgress;
  463. switch (temp){
  464. case GameStatesStatic.GameProgress.Race:
  465. this.type = 0;
  466. // cc.log("当前的typssssse","s1");
  467. break;
  468. case GameStatesStatic.GameProgress.LongJump:
  469. this.type = 1;
  470. // cc.log("当前的typssssse","s2");
  471. break;
  472. case GameStatesStatic.GameProgress.Hurdle:
  473. this.type = 2;
  474. // cc.log("当前的typssssse","s3");
  475. break;
  476. case GameStatesStatic.GameProgress.RideBike:
  477. this.type = 3;
  478. // cc.log("当前的typssssse","s4");
  479. break;
  480. case GameStatesStatic.GameProgress.Javelin:
  481. this.type = 4;
  482. // cc.log("当前的typssssse","s5");
  483. break;
  484. }
  485. },
  486. getSpeedLv : function () {
  487. var lv = this.Canvas.getComponent("NodeTouch").Hero.getComponent("HeroControl").PlayerStateScript.CurrentSpeed;
  488. // PlayerState.SlowSpeed
  489. // PlayerState.MiddleSpeed
  490. // PlayerState.HightSpeed
  491. // cc.log("现在是什么啊",PlayerState);
  492. // cc.log("当前速度",lv);
  493. var state = PlayerState.SpeedRangeForAnimation;
  494. if (lv >= state.SlowSpeed[0] && lv <= state.SlowSpeed[1]) {
  495. return 0;
  496. }
  497. if (lv >= state.MiddleSpeed[0] && lv <= state.MiddleSpeed[1]) {
  498. return 1;
  499. }
  500. if (lv >= state.HightSpeed[0] && lv <= state.HightSpeed[1]) {
  501. return 2;
  502. }
  503. return 0;
  504. }
  505. // start () {
  506. //
  507. // },
  508. // update (dt) {},
  509. });