player.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. onLoad () {
  31. },
  32. start () {
  33. },
  34. setMap : function(Map,type){
  35. this.actorsNode = Map;
  36. this._an(type);
  37. this._init();
  38. },
  39. _an :function (type){
  40. this.ansName = [];
  41. if (type == 0) {
  42. this.ansName.push("RedIdleAnim");
  43. this.ansName.push("RedJumpAnim");
  44. this.ansName.push("RedDownCryAnim");
  45. this.ansName.push("RedLoseCryAnim");
  46. this.ansName.push("RedWinAnim");
  47. }
  48. if (type == 1) {
  49. this.ansName.push("BlueIdleAnim");
  50. this.ansName.push("BlueJumpAnim");
  51. this.ansName.push("BlueDownCryAnim");
  52. this.ansName.push("BlueLoseCryAnim");
  53. this.ansName.push("BlueWinAnim");
  54. }
  55. },
  56. _init : function(){
  57. this.indexes = 0;
  58. this.addY = 0;
  59. this.node.setPosition(this.actorsNode[0].x,this.actorsNode[0].y+this.addY);
  60. this.inputTag = true;
  61. // 一步两步的 基本记录数组位置的索引
  62. this.indexes = 0;
  63. //记录所有操作 索引的数组
  64. this.Records = [];
  65. //按了多少次
  66. this.Recordsindex = 0;
  67. this.Records.push(this.indexes);
  68. this.player = UtilsNode.getNode("player",this.node);
  69. this.gameOverTag = true;
  70. },
  71. addOne : function () {
  72. if (!this.gameOverTag) {
  73. return;
  74. }
  75. if (!this.inputTag) {
  76. return;
  77. }
  78. let temp = this.indexes;
  79. this.Records.push(temp);
  80. this.indexes++;
  81. app.bgm.playha();
  82. this.playerSetPosition();
  83. },
  84. addTwo : function () {
  85. if (!this.gameOverTag) {
  86. return;
  87. }
  88. if (!this.inputTag) {
  89. return;
  90. }
  91. let temp = this.indexes;
  92. this.Records.push(temp);
  93. this.indexes+=2;
  94. app.bgm.playhei();
  95. this.playerSetPosition();
  96. },
  97. addAuto : function () {
  98. if (!this.gameOverTag) {
  99. return;
  100. }
  101. if (!this.inputTag) {
  102. return;
  103. }
  104. let temp = this.indexes;
  105. this.Records.push(temp);
  106. let actor = this.actorsNode[this.indexes+1].getComponent("actor");
  107. if (actor.type == 2) {
  108. let x = UtilsNode.getRandom(2,0,10);
  109. if (x < 8) {
  110. this.indexes+=2;
  111. }else{
  112. this.indexes+=1;
  113. }
  114. }else{
  115. this.indexes+=1;
  116. }
  117. this.playerSetPosition();
  118. },
  119. playerSetPosition : function () {
  120. if (this.indexes >= this.actorsNode.length-1) {
  121. this.indexes = this.actorsNode.length-1;
  122. }
  123. this.checkType();
  124. // console.log("记录数组日常遍历",this.Records);
  125. var finished = cc.callFunc(function(target, score) {
  126. // app.camera.y = this.node.y-640;
  127. // var myAction = cc.sequence(cc.moveTo(1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y)))
  128. // console.log("镜头现在高度",app.camera.y);
  129. // console.log("玩家现在高度",this.node.y);
  130. if (this.callBackCameraMove!=null) {
  131. this.callBackCameraMove(this.node);
  132. }
  133. if (this.callBackMove!=null) {
  134. this.callBackMove(this.indexes);
  135. }
  136. //播放动画
  137. this.playAnEnd();
  138. this.playAnEnd_bg();
  139. if (this.indexes==8) {
  140. if (this.callBackInfo) {
  141. this.callBackInfo();
  142. }
  143. }
  144. // setTimeout(function () {
  145. // this.inputTag = true;
  146. // }.bind(this),500);
  147. // console.log("现在的位置",this.node);
  148. }, this);//动作完成后会给玩家加100分
  149. // var myAction = cc.sequence(cc.jumpTo(1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y)), finished);
  150. var myAction = cc.sequence(
  151. cc.jumpTo(0.1, cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY-20), 200, 1),
  152. cc.moveTo(0.05,cc.v2(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY+10)),
  153. finished);
  154. // this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y);
  155. this.node.runAction(myAction);
  156. this.inputTag = false;
  157. this.playAnStart();
  158. },
  159. playAnStart : function(){
  160. let actor = this.actorsNode[this.indexes].getComponent("actor");
  161. let an = this.player.getComponent(cc.Animation);
  162. let Clips = an.getClips();
  163. console.log("当前动画列表",Clips);
  164. // console.log("地板什么类型的",actor.type);
  165. an.play(this.ansName[1]);
  166. // switch (actor.type) {
  167. // case 2:
  168. // if (this.actorsNode[this.indexes].userData==null ) {
  169. // let data = {type : true};
  170. // this.actorsNode[this.indexes].userData = data;
  171. // //要掉下去了
  172. // an.play();
  173. // }
  174. //
  175. // break;
  176. // case 0:
  177. // case 1:
  178. // an.play();
  179. // break;
  180. // case 3:
  181. // an.play();
  182. // break;
  183. // }
  184. },
  185. playAnEnd : function(){
  186. let actor = this.actorsNode[this.indexes].getComponent("actor");
  187. let an = this.player.getComponent(cc.Animation);
  188. let Clips = an.getClips();
  189. console.log("地板什么类型的",actor.type);
  190. let temp = app.bg.actorps[this.indexes];
  191. switch (actor.type) {
  192. case 2:
  193. // if (this.actorsNode[this.indexes].userData==null ) {
  194. // let data = {type : true};
  195. // this.actorsNode[this.indexes].userData = data;
  196. // //要掉下去了
  197. // an.play();
  198. // }
  199. an.play(this.ansName[2]);
  200. break;
  201. case 0:
  202. case 1:
  203. an.play();
  204. let temp1 = app.bg.actorps[this.indexes-1];
  205. console.log("落地之后这个是什么类型的",temp,"上个类型",temp1);
  206. if (temp.q != null || temp1.q!=null) {
  207. this.node.setScale(1,-1);
  208. this.node.setRotation(180);
  209. console.log("我应该脸往左",temp,temp1);
  210. }
  211. if (temp.w!= null || temp1.w!=null) {
  212. this.node.setScale(1,1);
  213. this.node.setRotation(0);
  214. console.log("我应该脸往右",temp,temp1);
  215. }
  216. if (this.callBackJump!=null) {
  217. this.callBackJump(temp);
  218. }
  219. break;
  220. case 3:
  221. if (this.callBackJump!=null) {
  222. this.callBackJump(temp);
  223. }
  224. an.play(this.ansName[4]);
  225. break;
  226. }
  227. },
  228. showsSmoke : function(b){
  229. let node = UtilsNode.getNode("qi",this.node);
  230. node.getComponent("kill").setActive(b);
  231. },
  232. playAnEnd_bg : function(){
  233. let actor = this.actorsNode[this.indexes].getComponent("actor");
  234. let an = this.actorsNode[this.indexes].getComponent("itemmanager");
  235. this.showsSmoke(true);
  236. console.log("地板什么类型的",actor.type);
  237. switch (actor.type) {
  238. case 2:
  239. if (this.actorsNode[this.indexes].userData==null ) {
  240. let data = {type : true};
  241. this.actorsNode[this.indexes].userData = data;
  242. //要掉下去了
  243. an.play();
  244. }
  245. an.playwater();
  246. break;
  247. case 0:
  248. case 1:
  249. an.play();
  250. break;
  251. case 3:
  252. an.play();
  253. break;
  254. }
  255. },
  256. startGame : function(b){
  257. this.gameOverTag = b;
  258. },
  259. //检查地板类型
  260. checkType : function () {
  261. let actor = this.actorsNode[this.indexes].getComponent("actor");
  262. let an = this.player.getComponent(cc.Animation);
  263. let Clips = an.getClips();
  264. console.log("地板什么类型的",actor.type);
  265. switch (actor.type) {
  266. case 2:
  267. //要掉下去了
  268. if (this.callBackTrap!=null) {
  269. this.callBackTrap();
  270. }
  271. this.inputTag = false;
  272. // console.log("记录数组陷阱",this.Records);
  273. setTimeout(function () {
  274. this.indexes = this.Records[this.Records.length-1];
  275. this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y+this.addY);
  276. UtilsNode.getNode("player",this.node).setPosition(0,0);
  277. this.inputTag = true;
  278. an.play(this.ansName[0]);
  279. this.playAnEnd_bg();
  280. }.bind(this),1500);
  281. break;
  282. case 0:
  283. case 1:
  284. this.inputTag = false;
  285. // console.log("记录数组陷阱",this.Records);
  286. setTimeout(function () {
  287. // this.indexes = this.Records[this.Records.length-1];
  288. // this.node.setPosition(this.actorsNode[this.indexes].x,this.actorsNode[this.indexes].y);
  289. this.inputTag = true;
  290. an.play(this.ansName[0]);
  291. }.bind(this),200);
  292. break;
  293. case 3:
  294. setTimeout(function () {
  295. if (this.callBackGameOver != null) {
  296. this.callBackGameOver();
  297. }
  298. // an.play(this.ansName[4]);
  299. this.winOrLose(true);
  300. // actor.setGameOverShow(false);
  301. let star = UtilsNode.getNode("star",this.actorsNode[this.indexes]);
  302. star.getComponent("endstart").endGame();
  303. }.bind(this),200);
  304. break;
  305. }
  306. },
  307. winOrLose : function(b){
  308. this.gameOverTag = false;
  309. let an = this.player.getComponent(cc.Animation);
  310. if (b) {
  311. an.play(this.ansName[4]);
  312. }else{
  313. an.play(this.ansName[3]);
  314. }
  315. },
  316. //todo 回调区域------------------------------------------------------
  317. callBackGameOver : function(){},
  318. setCallBackGameOver : function (callBackGameOver) {
  319. this.callBackGameOver = callBackGameOver;
  320. },
  321. callBackTrap : function(){},
  322. setCallBackTrap : function (callBackTrap) {
  323. this.callBackTrap = callBackTrap;
  324. },
  325. callBackInfo : function(){},
  326. setCallBackInfo : function (callBackInfo) {
  327. this.callBackInfo = callBackInfo;
  328. },
  329. callBackJump : function(){},
  330. setCallBackJump : function (callBackJump) {
  331. this.callBackJump = callBackJump;
  332. },
  333. callBackCameraMove : function(){},
  334. setCallBackCameraMove : function (callBackCameraMove) {
  335. this.callBackCameraMove = callBackCameraMove;
  336. },
  337. callBackMove : function(){},
  338. setCallBackMove : function (callBackMove) {
  339. this.callBackMove = callBackMove;
  340. },
  341. // update (dt) {},
  342. });