ZY_StarManager.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /**
  2. * 游戏核心部分
  3. */
  4. let GameEvent = require('GameEvent');
  5. var ZYGameTool = require('GameTool');
  6. let Blast = require('Blast');
  7. cc.Class({
  8. extends: require("ZY_GameLayer"),
  9. properties: {
  10. touchLayer: cc.Node,//触摸Layer
  11. clipDraw: cc.Node,//棋盘绘制节点
  12. clipDrawTemp: cc.Node,//棋盘绘制节点
  13. propNode: {
  14. tooltip: '道具',
  15. type: cc.Node,
  16. default: [],
  17. },
  18. leveNumLabel: cc.Label,//当前关卡
  19. targetScoreLabel: cc.Label,//目标分数
  20. scoreLabel: cc.Label,//当前分数
  21. coinNumLabel: cc.Label,//金币
  22. propTipNode: cc.Node,
  23. pauseBtn: cc.Node,
  24. gameEndLayer: cc.Prefab,
  25. labelTip: cc.Prefab,
  26. maskLayer: cc.Prefab,
  27. bombSpine: cc.Prefab,
  28. scoreNum: cc.Prefab,
  29. BLAST_PREFAB: {
  30. tooltip: '爆炸特效预制体',
  31. type: cc.Prefab,
  32. default: null,
  33. },
  34. topBg: cc.Node,
  35. isCanPauseTouch: true,
  36. isInitChessBg: false,
  37. },
  38. onLoad() {
  39. if (!Module.GameManager) Module.GameManager = this;
  40. this.node.getChildByName("chessBg").getChildByName("Bg").scale = 0;
  41. },
  42. start() {
  43. this.setGameState(GameState.STATE_STOP);
  44. zy.stopBgMusic();
  45. this.clipDraw.active = false;
  46. this.topBg.active = false;
  47. this.schedule(this.dataUpdate, 0.1);
  48. this.init();
  49. this.initData();
  50. this.initTouchLayer();
  51. // this.retainScoreMotion(8, 10, 3.0); //奖励多少分
  52. this.node.runAction(cc.sequence(new cc.delayTime(0.9), cc.callFunc(function () {
  53. this.starDrop();
  54. }.bind(this))));
  55. // this.gameFaild();
  56. //游戏顶部全面屏适配
  57. this.node.runAction(cc.sequence(cc.delayTime(0.01), cc.callFunc(function () {
  58. if (WINSIZE.width / WINSIZE.height < 0.53) {
  59. this.topBg.parent.y -= 60;
  60. this.node.getChildByName("btnNode").y -= 60;
  61. }
  62. this.topBg.active = true;
  63. this.topBg.y = this.topBg.y + 200;
  64. this.topBg.runAction(cc.moveBy(0.2, cc.v2(0, -200)));
  65. }.bind(this))));
  66. cc.game.on(cc.game.EVENT_HIDE, function () {
  67. if (!this.isCanPauseTouch){
  68. gameLocalData.dataClear();
  69. } else {
  70. gameLocalData.save();
  71. }
  72. console.log("游戏进入后台", this.isCanPauseTouch);
  73. // gameLocalData.dataClear();
  74. // this.onHideGame();//处理游戏切到后台时的事件
  75. }, this);
  76. cc.game.on(cc.game.EVENT_SHOW, function () {
  77. console.log("重新返回游戏");
  78. // this.onShowGame();//处理游戏切回前台时的事件
  79. }, this);
  80. },
  81. /**
  82. * 更新Label数据
  83. */
  84. dataUpdate: function () {
  85. this.leveNumLabel.string = this.getLevel().toString();
  86. this.targetScoreLabel.string = this.getTarget().toString();
  87. this.scoreLabel.string = this.getScore().toString();
  88. this.coinNumLabel.string = ZYGameTool.getInstance().getCoin().toString();
  89. },
  90. init: function () {
  91. this.BANER_HEIGHT = 180;
  92. heightoffest = this.BANER_HEIGHT + 10;
  93. if (WINSIZE.width / WINSIZE.height < 0.53) {
  94. heightoffest+=45;
  95. console.log("iphoneX");
  96. }
  97. this.coinNumLabel.string = coinNum.toString();
  98. this._guide = ZYGameTool.getInstance().getIntegerForKey(localDataItem.gameGuide, 1);
  99. this._guide = this._guide === 1 ? true : false;
  100. this._guidenumber = -1;
  101. if (this._guide) {
  102. this.showHelpLayer();
  103. }
  104. this.initBtn();
  105. //棋盘背景动画
  106. // this.node.getChildByName("chessBg").getChildByName("Bg").scaleY = 1;
  107. // this.node.getChildByName("chessBg").getChildByName("Bg").runAction(cc.sequence(cc.delayTime(0.3), cc.scaleTo(0.5, 1)));
  108. this.initProp();
  109. },
  110. showHelpLayer: function () {
  111. cc.game.emit(GameEvent.EventType.GANE_HELP);
  112. },
  113. initBtn: function () {
  114. //暂停按钮
  115. this.pauseBtn.on('click', function (event) {
  116. this.pauseClick();
  117. }.bind(this));
  118. },
  119. pauseClick: function () {
  120. if (!this.isCanPauseTouch) return;
  121. this.node.runAction(cc.sequence(cc.delayTime(0.1), cc.callFunc(function () {
  122. gameLocalData.save(this);
  123. }.bind(this))));
  124. cc.game.emit(GameEvent.EventType.GANE_PAUSE);
  125. },
  126. initProp: function () {
  127. // this.propTipNode.active = true;
  128. for (var i = 0; i < 2; i++) {
  129. this._propNode[i] = this.propNode[i];
  130. this._propmotion[i] = this.propNode[i].getChildByName("effect");
  131. this._proptipsp[i] = this.propTipNode.getChildByName("propTip" + (i + 1));
  132. }
  133. },
  134. initChess: function () {
  135. this.isInitChessBg = true;
  136. this.node.getChildByName("chessBg").y += (heightoffest - 10);
  137. this.node.getChildByName("chessBg").getChildByName("Bg").scale = 1;
  138. this.clipDraw.active = true;
  139. },
  140. /**
  141. * 恢复上次存档
  142. */
  143. setLastData: function () {
  144. gameLocalData.getSaveData();
  145. var lastdata = gameLocalData;
  146. if (lastdata._issaved) {
  147. this._firstsavein = true;
  148. this.setLevel(lastdata._level);
  149. this.setScore(lastdata._score);
  150. this.setTarget(lastdata._target);
  151. this._lastLevelScore = lastdata._lastscore;
  152. var chessinfo = lastdata._chesspad;
  153. for (var i = 0; i < STAR_WIDTH; i++)
  154. for (var j = 0; j < STAR_HEIGHT; j++) {
  155. if (parseInt(chessinfo[i * STAR_HEIGHT + j]) > 0) {
  156. this._chessInfo[i * STAR_HEIGHT + j] = parseInt(chessinfo[i * STAR_HEIGHT + j]);
  157. }
  158. }
  159. this.node.runAction(cc.sequence(cc.delayTime(1.5), cc.callFunc(function () {
  160. this.handleHandle();
  161. }.bind(this))));
  162. }
  163. },
  164. /**
  165. * 初始化数据信息
  166. */
  167. initData: function () {
  168. this._candealwith = true;
  169. this.setLevel(1);
  170. this.setScore(0);
  171. this.setTarget(this.getTargetScoreByRound(1));
  172. this._lastLevelScore = 0;
  173. for (var i = 0; i < STAR_WIDTH; i++)
  174. for (var j = 0; j < STAR_HEIGHT; j++)
  175. this._chessInfo[i * STAR_HEIGHT + j] = 0;
  176. this._selectbox = [];
  177. this._eliminateNum = 0;
  178. this._eliminatePos = [];
  179. this.setLastData();
  180. },
  181. /**
  182. * 分数算法
  183. * @param level
  184. * @returns {number}
  185. */
  186. getTargetScoreByRound: function (level) {
  187. var targetscore = 0;
  188. if (level == 1)
  189. targetscore = 1000;
  190. else
  191. targetscore = 10 * level * level + 1950 * level - 1440;
  192. return targetscore;
  193. },
  194. /**
  195. * 设置游戏状态
  196. * @param state
  197. */
  198. setGameState: function (state) {
  199. this._gamestate = state;
  200. },
  201. /**
  202. * 生成棋盘
  203. */
  204. starDrop: function () {
  205. this.isCanPauseTouch = true;
  206. this.setGameState(GameState.STATE_STOP);
  207. this._max = this.getTarget() - this.score;
  208. cc.game.emit(GameEvent.EventType.GAME_PROGRESS, 0);
  209. if (!this._firstsavein) {
  210. for (var i = 0; i < STAR_WIDTH; i++)
  211. for (var j = 0; j < STAR_HEIGHT; j++) {
  212. var color = zy.randNum(1, 5);
  213. this._chessInfo[i * STAR_HEIGHT + j] = color;
  214. }
  215. }
  216. var time = this.chessAppear(6);//zy.randNum(1, 6)
  217. console.log("time: ", time);
  218. this.node.runAction(new cc.sequence(new cc.delayTime(time), new cc.callFunc(function () {
  219. this.setGameState(GameState.STATE_RUN);
  220. }.bind(this))));
  221. },
  222. /**
  223. * 棋盘出现特效
  224. * @param val 特效类型
  225. * @returns {number}
  226. */
  227. chessAppear: function (val) {
  228. if (!this.isInitChessBg) this.initChess();
  229. var time = [0.1 * STAR_HEIGHT + 0.3, 0.4, 0.4, 1.1, 0.2, 0.3, 0.6, 0.02 * STAR_HEIGHT * STAR_WIDTH + 0.25];
  230. for (var i = 0; i < STAR_WIDTH; i++)
  231. for (var j = 0; j < STAR_HEIGHT; j++) {
  232. var id = this._chessInfo[i * STAR_HEIGHT + j];
  233. if (id > 0) {
  234. var box = zy.getSprite("game/star/starchess" + (id) );
  235. box.setScale(STAR_CHESS_SCALE);
  236. this.clipDraw.addChild(box, CHESSMAN_LEVEL, this.getBoxTag((i * STAR_HEIGHT + j)));
  237. var pos = this.chessPos(i, j);
  238. box.setPosition(cc.v2(pos.x, pos.y + STAR_HEIGHT * STAR_SIZE_WIDTH));
  239. // if((i*STAR_HEIGHT+j)==this._limitproptag){
  240. // box.setTexture(url_xmtg+ "res/animation/starpng/block_"+id+"_lb.png");
  241. // }
  242. if (val == 0) {
  243. box.setPosition(cc.v2(pos.x, pos.y + STAR_HEIGHT * STAR_SIZE_WIDTH));
  244. box.runAction(cc.sequence(cc.delayTime(j * 0.1 + zy.randNum(0, 9) * 0.01), cc.moveBy(0.2, cc.v2(0, -STAR_SIZE_WIDTH * STAR_HEIGHT))));
  245. } else if (val == 1) {
  246. box.setPosition(cc.v2(pos.x - STAR_WIDTH * STAR_SIZE_WIDTH, pos.y));
  247. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos)));
  248. } else if (val == 2) {
  249. box.setPosition(cc.v2(pos.x + STAR_WIDTH * STAR_SIZE_WIDTH, pos.y));
  250. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos)));
  251. } else if (val == 3) {
  252. box.setPosition(pos);
  253. box.opacity = 0;
  254. box.runAction(cc.sequence(cc.delayTime(0), cc.fadeIn(1.0)));
  255. } else if (val == 4) {
  256. if (i < STAR_WIDTH / 2.0) {
  257. box.setPosition(cc.v2(pos.x - (STAR_WIDTH * STAR_SIZE_WIDTH / 2.0), pos.y));
  258. } else {
  259. box.setPosition(cc.v2(pos.x + (STAR_WIDTH * STAR_SIZE_WIDTH / 2.0), pos.y));
  260. }
  261. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos)));
  262. } else if (val == 5) {
  263. if (j < STAR_HEIGHT / 2.0) {
  264. box.setPosition(cc.v2(pos.x, pos.y - (STAR_HEIGHT * STAR_SIZE_WIDTH / 2.0)));
  265. } else {
  266. box.setPosition(cc.v2(pos.x, pos.y + (STAR_HEIGHT * STAR_SIZE_WIDTH / 2.0)));
  267. }
  268. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos)));
  269. } else if (val == 6) {
  270. var k = 1;
  271. if ((j % 2) == 0)
  272. k = -1;
  273. box.setPosition(cc.v2(pos.x + k * STAR_WIDTH * STAR_SIZE_WIDTH, pos.y));
  274. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.5, pos)));
  275. }
  276. }
  277. }
  278. return time[val];
  279. },
  280. chessPos: function (i, j) {
  281. return cc.v2(WINSIZE.width / 2.0 + (i + 0.5 - STAR_WIDTH / 2.0) * STAR_SIZE_WIDTH, heightoffest + (j + 0.5) * STAR_SIZE_WIDTH);
  282. },
  283. /**
  284. * 获取游戏状态
  285. * @returns {number}
  286. */
  287. getGameState: function () {
  288. return this._gamestate;
  289. },
  290. /**
  291. * 初始化监听层
  292. */
  293. initTouchLayer: function () {
  294. this.touchLayer.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan.bind(this), this);
  295. this.touchLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoved.bind(this), this);
  296. this.touchLayer.on(cc.Node.EventType.TOUCH_END, this.onTouchEnded.bind(this), this);
  297. this.touchLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnded.bind(this), this);
  298. },
  299. txtTip: function (str) {
  300. var labelTip = cc.instantiate(this.labelTip);
  301. labelTip.parent = this.node;
  302. labelTip.scale = 0.7;
  303. labelTip.setPosition(cc.v2(0, 0));
  304. labelTip.getComponent(cc.Label).string = str.toString();
  305. labelTip.runAction(cc.sequence(cc.moveBy(0.5, cc.v2(0, WINSIZE.height / 2 - 50)), cc.delayTime(1.2), cc.removeSelf(true)));
  306. return labelTip;
  307. },
  308. /**
  309. * 打开提示界面
  310. */
  311. openTip: function(){
  312. cc.game.emit(GameEvent.EventType.GAME_TIP);
  313. },
  314. /**
  315. * 开始监听
  316. * @param event
  317. * @returns {boolean}
  318. */
  319. onTouchBegan: function (event) {
  320. cc.log("onTouchBegan");
  321. var position = event.getLocation();
  322. var target = this;
  323. if (target.getGameState() != GameState.STATE_RUN) {
  324. return false;
  325. }
  326. target._firstsavein = false;
  327. var k = target.propTouch(position);
  328. if (k > 0 && target._eliminateNum == 0) {
  329. if (PROPPRICE[k - 1] > coinNum) {
  330. this._selectprop = 0;
  331. // this.txtTip("金币不足");
  332. this.showPropTip(0);
  333. this.openTip();
  334. return false;
  335. }
  336. target.propMotion(k);
  337. return true;
  338. }
  339. if (position.x >= 0 && position.x < WINSIZE.width && position.y >= heightoffest && position.y <= (heightoffest + STAR_SIZE_WIDTH * STAR_HEIGHT)) {
  340. var pos = target.chessPt(position);
  341. if (target._chessInfo[pos.x * STAR_HEIGHT + pos.y] && ZYGameTool.getInstance().posIndexOf(target._eliminatePos, pos) == -1) {
  342. // if (target._selectprop == 2) target._selectprop =4 ;
  343. var temp = true;
  344. var vec = [];
  345. if (target._selectprop == 1 || target._selectprop == 2) {
  346. temp = target.propProcess(target._selectprop - 1, pos);
  347. // if(target._guide){
  348. // target.removeChildByTag(TIPFONTTAG);
  349. // target._guidenumber++;
  350. // }
  351. } else if (target._selectprop == 0) {
  352. target.boxTip(pos, vec);
  353. }
  354. var selectnumber = vec.length;
  355. if (selectnumber > 1 || (target._selectprop > 0 && temp)) {
  356. target.endTip();
  357. var ft = 0.0;
  358. if (target._selectprop > 0) {
  359. // ELSGameLogic.getInstance().gameCount("item","prop_"+target._selectprop);
  360. }
  361. if (target._selectprop == 0) {
  362. target._eliminateNum++;
  363. target.boxEliminate(vec);
  364. ft = boxremovesingletime * selectnumber + cleardelaytime;
  365. } else if (target._selectprop == 1) {
  366. ft = boxremovesingletime + 0.9;
  367. } else if (target._selectprop == 2) {
  368. ft = target._selectbox.length * 0.15;
  369. target.useProps4(pos);
  370. }
  371. if (target._selectprop == 1 || target._selectprop == 2) {
  372. target.setGameState(GameState.STATE_STOP);
  373. target.node.runAction(cc.sequence(cc.delayTime(ft), cc.callFunc(function () {
  374. target.touchHandle();
  375. }.bind(target))));
  376. target._selectprop = 0;
  377. target._selectbox = [];
  378. }
  379. }
  380. }
  381. return true;
  382. }
  383. return false;
  384. },
  385. //获取道具坐标
  386. propTouch: function (pos) {
  387. for (var i = 0; i < 2; i++) {
  388. if (this._propNode[i].getBoundingBoxToWorld().contains(pos)) return i + 1;
  389. }
  390. return 0;
  391. },
  392. //获取道具位置
  393. getPropPos: function (key) {
  394. if (key > 0 && key < 2) {
  395. return this._propNode[key - 1].parent.convertToWorldSpaceAR(this._propNode[key - 1].getPosition());
  396. }
  397. return cc.v2(0, 0);
  398. },
  399. useProps4: function (pos) {
  400. // var maskLayer = cc.instantiate(this.maskLayer);
  401. // maskLayer.zIndex = 99;
  402. // maskLayer.parent = this.node.parent;
  403. // this.clipDrawTemp.zIndex = 100;
  404. this.node.getChildByName("maskLayer").active =true;
  405. // this.node.getChildByName("maskLayer").zIndex = 99;
  406. var tempk = this._selectbox[0];
  407. for (var i in this._selectbox) {
  408. if (this._selectbox[i].x == pos.x && this._selectbox[i].y == pos.y) {
  409. this._selectbox[i].x = tempk.x;
  410. this._selectbox[i].y = tempk.y;
  411. this._selectbox[0].x = pos.x;
  412. this._selectbox[0].y = pos.y;
  413. break;
  414. }
  415. }
  416. var selectbox = this._selectbox.slice(1, this._selectbox.length);
  417. selectbox.sort(function (a, b) {
  418. return Math.random() > 0.5 ? -1 : 1;
  419. });
  420. selectbox.splice(0, 0, this._selectbox[0]);
  421. var actions = [];
  422. for (var i = 0; i < selectbox.length; i++) {
  423. var key1 = selectbox[i];
  424. var node = this.getClipItem(key1.x * STAR_HEIGHT + key1.y);
  425. node.index = 101;
  426. actions.push(cc.callFunc(function (target, key) {
  427. this.addParticle(key);
  428. this.removeBox(key);
  429. }.bind(this), this, key1));
  430. if (i == selectbox.length - 1) {
  431. break;
  432. }
  433. var key2 = selectbox[i + 1];
  434. var position1 = this.chessPos(key1.x, key1.y);
  435. var position2 = this.chessPos(key2.x, key2.y);
  436. actions.push(cc.callFunc(function (target, data) {
  437. // var line=new cc.Sprite(starpng[wb.randNum(14,18)]);
  438. var line = zy.getSprite("game/prop/starprope" + zy.randNum(1, 5));
  439. line.setAnchorPoint(cc.v2(0, 0.5));
  440. var length = Math.sqrt(Math.abs(data.pos1.x - data.pos2.x) * Math.abs(data.pos1.x - data.pos2.x) + Math.abs(data.pos1.y - data.pos2.y) * Math.abs(data.pos1.y - data.pos2.y));
  441. var wid = 70;
  442. var lengthscale = length * 1.0 / wid;
  443. line.scaleX = 0.06 * lengthscale;
  444. cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 1); //消除个数
  445. line.runAction(cc.sequence(cc.scaleTo(0.1, 1.0 * lengthscale, 1.5), cc.scaleTo(0.25, 1.0 * lengthscale, 0.06 * 1.5), cc.callFunc(function () {
  446. line.removeFromParent();
  447. })));
  448. line.setPosition(data.pos1);
  449. var angle1 = Math.atan2(data.pos2.y - data.pos1.y, data.pos2.x - data.pos1.x);
  450. var angle = -1 * cc.misc.radiansToDegrees(angle1);
  451. line.setRotation(angle);
  452. this.clipDrawTemp.addChild(line, 102);
  453. var score = this.getScoreByNum(1);
  454. this.updateScore(score);
  455. }.bind(this), this, {pos1: position1, pos2: position2}));
  456. actions.push(cc.delayTime(0.15));
  457. }
  458. actions.push(cc.callFunc(function () {
  459. // maskLayer.removeFromParent();
  460. this.node.getChildByName("maskLayer").active =false;
  461. this.clipDrawTemp.removeAllChildren(true);
  462. this.showPropTip(0);
  463. }.bind(this)));
  464. this.node.runAction(cc.sequence(actions));
  465. },
  466. //使用道具
  467. propProcess: function (id, pos, key) {
  468. if (!pos) {
  469. pos = cc.v2(0, 0);
  470. }
  471. if (!key) {
  472. key = 1;
  473. }
  474. if (this._selectprop > 0) {
  475. this.showPropMotion(this._selectprop, 0);
  476. }
  477. //
  478. if (key == 1) {
  479. console.log("id: ", id);
  480. if (PROPPRICE[id] > coinNum) {
  481. this._selectprop = 0;
  482. this.txtTip();
  483. return false;
  484. } else {
  485. //使用道具扣除金币
  486. this.txtTip("Use props to deduct gold coins " + PROPPRICE[id]);
  487. ZYGameTool.getInstance().useCoin(-PROPPRICE[id]);
  488. }
  489. }
  490. if (id == 1) {
  491. var key = this.getChessInfo(pos);
  492. console.log("---key--: ", key);
  493. for (var i = 0; i < STAR_WIDTH; i++)
  494. for (var j = 0; j < STAR_HEIGHT; j++) {
  495. if (key == this.getChessInfo(cc.v2(i, j))) {
  496. this._selectbox.push(cc.v2(i, j));
  497. }
  498. }
  499. } else if (id == 0) {
  500. // if(id==0){
  501. var ssp = zy.getSprite("game/prop/prop1");
  502. this.clipDraw.addChild(ssp, 1000);
  503. ssp.setPosition(this.getPropPos(1));
  504. var v1 = ssp.getPosition();
  505. var tempv = this.chessPos(pos.x, pos.y);
  506. var v2 = cc.v2(tempv.x + 10, tempv.y - 10);
  507. var v3 = cc.v2(0, 0);
  508. if (v2.x < WINSIZE.width / 2)
  509. v3 = cc.v2(v1.x + 200, v1.y - 100);
  510. else
  511. v3 = cc.v2(v1.x + 200, v1.y + 100);
  512. var bezierpath = [v1, v3, v2];
  513. var bezier = cc.bezierTo(0.7, bezierpath);
  514. var bombSpine = this.createBombSpine(tempv, this.clipDraw);
  515. bombSpine.active = false;
  516. var bombAction = cc.callFunc(function () {
  517. bombSpine.active = true;
  518. });
  519. cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 6); //消除个数
  520. ssp.runAction(cc.sequence(bezier, bombAction, cc.delayTime(0.2), cc.callFunc(function (target, tempv) {
  521. bombSpine.removeFromParent();
  522. ssp.removeFromParent();
  523. }.bind(this), this, tempv)));
  524. ssp.runAction(cc.scaleTo(0.15, 1.5));
  525. this.node.runAction(cc.sequence(cc.delayTime(0.7), cc.callFunc(function (target, pos) {
  526. this.node.runAction(cc.sequence(cc.delayTime(0.0), cc.callFunc(function (target, pos) {
  527. var temp = 0;
  528. for (var i = pos.x - 1; i <= pos.x + 1; i++) {
  529. for (var j = pos.y - 1; j <= pos.y + 1; j++) {
  530. if (i >= 0 && i < STAR_WIDTH && j >= 0 && j < STAR_HEIGHT && this.getChessInfo(cc.v2(i, j))) {
  531. this.addParticle(cc.v2(i, j));
  532. this.removeBox(cc.v2(i, j));
  533. ++temp;
  534. }
  535. }
  536. }
  537. var score = this.getScoreByNum(temp);
  538. this.updateScore(score);
  539. }.bind(this), this, pos)));
  540. this.showPropTip(0);
  541. }.bind(this), this, pos)));
  542. }
  543. return true;
  544. },
  545. createBombSpine: function (pos, node, index) {
  546. if (index == undefined) index = 10000;
  547. var bombSpine = cc.instantiate(this.bombSpine);
  548. bombSpine.parent = node;
  549. bombSpine.zIndex = index;
  550. bombSpine.setPosition(pos);
  551. return bombSpine;
  552. },
  553. propMotion: function (k) {
  554. if (this._selectprop > 0) {
  555. this.showPropMotion(this._selectprop, 0);
  556. }
  557. cc.log("match touch began2", k);
  558. if (this._selectprop == k) {
  559. this._selectprop = 0;
  560. this.showPropTip(0);
  561. } else {
  562. this._selectprop = k;
  563. this.showPropTip(k);
  564. this.showPropMotion(k, 1);
  565. }
  566. },
  567. showPropTip: function (k) {
  568. if (k == 0) {
  569. this.propTipNode.active = false;
  570. } else if (k > 0 && k < 3) {
  571. this.propTipNode.active = true;
  572. for (var i = 0; i < 2; i++) {
  573. if (i == (k - 1)) {
  574. this._proptipsp[i].active = true;
  575. } else {
  576. this._proptipsp[i].active = false;
  577. }
  578. }
  579. }
  580. },
  581. //道具点击动画
  582. showPropMotion: function (key, type) {
  583. if (type == 1) {
  584. this._propmotion[key - 1].active = true;
  585. this._propmotion[key - 1].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.0, 1.0),
  586. cc.scaleTo(1.0, 1.5))));
  587. this._propmotion[key - 1].runAction(cc.repeatForever(cc.sequence(cc.fadeIn(0.0),
  588. cc.fadeOut(1.0))));
  589. } else if (type == 0) {
  590. this._propmotion[key - 1].active = false;
  591. this._propmotion[key - 1].stopAllActions();
  592. this._propmotion[key - 1].setScale(1.0);
  593. this._propmotion[key - 1].opacity = 255;
  594. }
  595. },
  596. getScoreByNum: function (num) {
  597. var k = 5;
  598. var t = 5;
  599. for (var i = 0; i < num; i++) {
  600. k += t;
  601. t += 10;
  602. }
  603. return k;
  604. },
  605. updateScore: function (score) {
  606. this.score += score;
  607. },
  608. scoreMotion: function (score, pt) {
  609. var scoreNum = cc.instantiate(this.scoreNum);
  610. scoreNum.parent = this.clipDraw;
  611. scoreNum.zIndex = 10000;
  612. scoreNum.getComponent(cc.Label).string = score.toString();
  613. scoreNum.setPosition(this.chessPos(pt.x, pt.y));
  614. scoreNum.runAction(cc.sequence(cc.moveBy(1.0, cc.v2(0, STAR_SIZE_WIDTH)), cc.callFunc(function () {
  615. scoreNum.removeFromParent();
  616. })));
  617. },
  618. /**
  619. * 连消提示
  620. * @param vec
  621. */
  622. boxEliminate: function (vec) {
  623. var score = this.getScoreByNum(vec.length);
  624. this.updateScore(score);
  625. console.log("--score--: ", score);
  626. this.scoreMotion(score, vec[0]);
  627. if (this.getScore() >= this.getTarget() && (this.getScore() - score) < this.getTarget()) {
  628. this.spriteShow(zy_gxpng[0]);
  629. } else {
  630. if (vec.length == 5) {
  631. this.spriteShow("game/main_tip/great");
  632. } else if (vec.length > 5) {
  633. this.spriteShow("game/main_tip/zan");
  634. }
  635. }
  636. this.levelupmusic = 0;
  637. this.boxSequence(vec, true);
  638. var arrayOfActions = [];
  639. for (var i in vec) {
  640. if (i != "0") {
  641. arrayOfActions.push(cc.delayTime(boxremovesingletime));
  642. }
  643. var key = vec[i];
  644. arrayOfActions.push(cc.callFunc(function (target, data) {
  645. this.addParticle(data);
  646. this.removeBox(data);
  647. }.bind(this), this, key));
  648. }
  649. arrayOfActions.push(cc.delayTime(cleardelaytime));
  650. arrayOfActions.push(cc.callFunc(function () {
  651. this._eliminateNum--;
  652. if (this._eliminateNum == 0) {
  653. this._eliminatePos = [];
  654. this.setGameState(GameState.STATE_STOP);
  655. this.touchHandle();
  656. }
  657. }.bind(this)));
  658. this.node.runAction(cc.sequence(arrayOfActions));
  659. },
  660. spriteShow: function (str) {
  661. if (str == zy_gxpng[0]) {
  662. var endpos = cc.v2(0, WINSIZE.height * Hkey - 180);
  663. var spr = zy.getSprite(str);
  664. this.node.addChild(spr, 1000, "777");
  665. spr.setPosition(cc.v2(0, 0 + heightoffest / 2.0));
  666. spr.runAction(cc.sequence(cc.fadeOut(0.08), cc.fadeIn(0.08), cc.fadeOut(0.08), cc.fadeIn(0.08), cc.fadeOut(0.08), cc.fadeIn(0.08),
  667. cc.scaleTo(0.2, 0.8), cc.delayTime(0.52), cc.moveTo(0.5, endpos)));
  668. spr.runAction(cc.sequence(new cc.delayTime(1.2), cc.scaleTo(0.5, 0.25), cc.callFunc(function () {
  669. spr.removeFromParent();
  670. var sp = zy.getSprite(str);
  671. sp.setScale(0.25);
  672. // this.getParent().getBackLayer().getChildByName("node").addChild(sp,10,777);
  673. this.node.addChild(spr, 12, "777");
  674. sp.setPosition(cc.v2(-170, -175));
  675. }.bind(this))));
  676. } else {
  677. this.spriteShow2(str, cc.v2(0, 0));
  678. }
  679. },
  680. spriteShow2: function (res, pos) {
  681. var sp = zy.getSprite(res);
  682. this.node.addChild(sp, 1000);
  683. sp.setPosition(pos);
  684. sp.setScale(1.0);
  685. if (res == "game/main_tip/xsyd") {
  686. sp.runAction(sequence(cc.moveBy(0.5, cc.v2(0, 50)),
  687. cc.delayTime(0.2), cc.fadeOut(0.3), cc.callFunc(function () {
  688. sp.removeFromParent();
  689. })));
  690. } else {
  691. sp.runAction(cc.sequence(cc.fadeOut(0.08), cc.fadeIn(0.08), cc.fadeOut(0.08), cc.fadeIn(0.08), cc.fadeOut(0.08), cc.fadeIn(0.08),
  692. cc.scaleTo(0.2, 0.8), cc.delayTime(0.2), cc.fadeOut(0.3), cc.callFunc(function () {
  693. sp.removeFromParent();
  694. })));
  695. }
  696. },
  697. /**
  698. * 可以消除的元素放入_eliminatePos
  699. * @param pos
  700. * @param vect
  701. */
  702. boxTip: function (pos, vect) {
  703. var vec = this.boxGet(pos);
  704. if (vec.length > 1) {
  705. for (var i in vec) {
  706. vect.push(vec[i]);
  707. this._eliminatePos.push(vec[i]);
  708. var box = this.getClipItem(vec[i].x * STAR_HEIGHT + vec[i].y);
  709. this.setBoxMotion(box, true);
  710. }
  711. }
  712. cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, vect.length); //消除个数
  713. },
  714. getClipItem: function (tag) {
  715. return this.clipDraw.getChildByName(tag.toString());
  716. },
  717. //TAG值转换为String类型
  718. getBoxTag: function getBoxTag(v) {
  719. return v.toString();
  720. },
  721. /**
  722. * 消除结束
  723. */
  724. endTip: function () {
  725. this._clicktiptime = 0;
  726. for (var i in this._clicktipvec) {
  727. var key = this._clicktipvec[i];
  728. var box = this.getClipItem(key.x * STAR_HEIGHT + key.y);
  729. this.setBoxMotion(box, false);
  730. }
  731. this._clicktipvec = [];
  732. },
  733. /**
  734. * 获取可以消除的元素
  735. * @param pos
  736. * @returns {[]}
  737. */
  738. boxGet: function (pos) {
  739. var vec = [];
  740. this.boxAdd(pos, vec);
  741. return vec;
  742. },
  743. /**
  744. * 树状搜索消除元素
  745. * @param pos
  746. * @param vec
  747. */
  748. boxAdd: function (pos, vec) {
  749. vec.push(pos);
  750. //x-1
  751. if ((pos.x - 1) >= 0 && ZYGameTool.getInstance().posIndexOf(vec, cc.v2(pos.x - 1, pos.y)) == -1 && (this.getChessInfo(pos) == this.getChessInfo(cc.v2(pos.x - 1, pos.y)))) {
  752. this.boxAdd(cc.v2(pos.x - 1, pos.y), vec);
  753. }
  754. //x+1
  755. if ((pos.x + 1) < STAR_WIDTH && ZYGameTool.getInstance().posIndexOf(vec, cc.v2(pos.x + 1, pos.y)) == -1 && (this.getChessInfo(pos) == this.getChessInfo(cc.v2(pos.x + 1, pos.y)))) {
  756. this.boxAdd(cc.v2(pos.x + 1, pos.y), vec);
  757. }
  758. //y-1
  759. if ((pos.y - 1) >= 0 && ZYGameTool.getInstance().posIndexOf(vec, cc.v2(pos.x, pos.y - 1)) == -1 && (this.getChessInfo(pos) == this.getChessInfo(cc.v2(pos.x, pos.y - 1)))) {
  760. this.boxAdd(cc.v2(pos.x, pos.y - 1), vec);
  761. }
  762. //y+1
  763. if ((pos.y + 1) < STAR_HEIGHT && ZYGameTool.getInstance().posIndexOf(vec, cc.v2(pos.x, pos.y + 1)) == -1 && (this.getChessInfo(pos) == this.getChessInfo(cc.v2(pos.x, pos.y + 1)))) {
  764. this.boxAdd(cc.v2(pos.x, pos.y + 1), vec);
  765. }
  766. },
  767. /**
  768. * 星星消除给个动作
  769. * @param box
  770. * @param b
  771. */
  772. setBoxMotion: function (box, b) {
  773. if (box) {
  774. if (b) {
  775. box.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(1.0, 0.89), cc.scaleTo(1.0, 1.0))));
  776. } else {
  777. box.stopAllActions();
  778. box.setScale(1.0);
  779. }
  780. }
  781. },
  782. /**
  783. * 通过坐标找到目标对象
  784. * @param pos
  785. * @returns {cc.Vec2}
  786. */
  787. chessPt: function (pos) {
  788. var x = parseInt((pos.x - WINSIZE.width / 2.0) / STAR_SIZE_WIDTH + STAR_WIDTH / 2.0);
  789. var y = parseInt((pos.y - heightoffest) / STAR_SIZE_WIDTH);
  790. return cc.v2(x, y);
  791. },
  792. touchHandle: function () {
  793. var time = 0.0;
  794. if (this.chessMove()) {
  795. time += chessmovetime;
  796. }
  797. this.node.runAction(cc.sequence(cc.delayTime(time), cc.callFunc(function () {
  798. if (this._candealwith) {
  799. this.handleHandle();
  800. }
  801. }.bind(this))));
  802. },
  803. handleHandle: function () {
  804. if (this.chessCanEliminate()) {
  805. this.setGameState(GameState.STATE_RUN);
  806. } else {
  807. this.node.runAction(cc.sequence(cc.delayTime(0.5), new cc.callFunc(function () {
  808. this.chessClear();
  809. }.bind(this))))
  810. }
  811. },
  812. chessClear: function () {
  813. this.isCanPauseTouch = false;
  814. var eliminatebox = [];
  815. for (var i = 0; i < STAR_WIDTH; i++)
  816. for (var j = 0; j < STAR_HEIGHT; j++) {
  817. if (this.getChessInfo(cc.v2(i, j))) {
  818. eliminatebox.push(cc.v2(i, j));
  819. }
  820. }
  821. if (eliminatebox.length > 0) {
  822. eliminatebox.sort(function (a, b) {
  823. return Math.random() > 0.5 ? -1 : 1;
  824. })
  825. }
  826. this.levelupmusic = 0;
  827. this.boxSequence(eliminatebox, false);
  828. var num = this.getRetainScore(eliminatebox.length);
  829. this.updateScore(num);
  830. // ELSGameData.getInstance().setDayHighScore(this._score,num);
  831. this.scoreMotion(num, cc.v2(WINSIZE.width / 2, WINSIZE.height / 2));
  832. // ELSGameLogic.getInstance().showHyRank(this._score,false);
  833. var ftime = this.smallPeopleCollect(eliminatebox);
  834. this.retainScoreMotion(eliminatebox.length, num, 3.0); //奖励多少分
  835. var ft = Math.max(ftime, 3);
  836. if (this.getScore() > this.getTarget() && (this.getScore() - num) < this.getTarget()) {
  837. this.spriteShow(zy_gxpng[0]);
  838. }
  839. // gameLocalData.save(this);
  840. this.node.runAction(cc.sequence(cc.delayTime(ft), new cc.callFunc(function () {
  841. this.gameEnd();
  842. }.bind(this))));
  843. },
  844. retainScoreMotion: function (number, score, ft) {
  845. var fnt1 = cc.instantiate(this.labelTip);
  846. fnt1.zIndex = 100;
  847. fnt1.parent = this.node;
  848. fnt1.getComponent(cc.Label).string = "reward " + score + "score";
  849. fnt1.x = -(WINSIZE.width / 2 + 100);
  850. fnt1.y = heightoffest * 0.5;
  851. fnt1.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.2, cc.v2(0, heightoffest * 0.5))));
  852. var fnt2 = cc.instantiate(this.labelTip);
  853. fnt2.zIndex = 100;
  854. fnt2.parent = this.node;
  855. fnt2.getComponent(cc.Label).string = "surplus " + number + "blocks";
  856. fnt2.x = -(WINSIZE.width / 2 + 100);
  857. fnt2.y = heightoffest * 0.5 - 100;
  858. fnt2.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.2, cc.v2(0, heightoffest * 0.5 - 100)), cc.delayTime(ft - 0.2), cc.callFunc(function () {
  859. fnt1.removeFromParent();
  860. fnt2.removeFromParent();
  861. })));
  862. },
  863. /**
  864. * 游戏结束
  865. */
  866. gameEnd: function () {
  867. // this.isCanPauseTouch = true;
  868. cc.game.emit(GameEvent.EventType.GAME_PASS_STAGE); //消除个数
  869. //闯关失败
  870. if (this.getScore() < this.getTarget()) {
  871. // this.gameFaild();
  872. cc.game.emit(GameEvent.EventType.GANE_FAIL);
  873. // cc.director.loadScene("homeScene");
  874. } else {
  875. //闯关成功
  876. // this.LevelUpMotion();
  877. if (this.getScore() >= this.getTarget()) {
  878. this.spriteShow(zy_gxpng[0]);
  879. }
  880. this.setLevel(this.getLevel() + 1);
  881. this.setTarget(this.getTargetScoreByRound(this.getLevel()));
  882. this._lastLevelScore = this.getScore();
  883. this.node.runAction(cc.sequence(cc.delayTime(0.8), cc.callFunc(function () {
  884. this._firstsavein = false;
  885. this.starDrop();
  886. }.bind(this))));
  887. }
  888. },
  889. clearData: function () {
  890. this.score = 0;
  891. this._lastLevelScore = 0;
  892. this.level = 1;
  893. this.target = 0;
  894. this.hang = 0;
  895. this.chessInfo = [];//记录棋盘元素的颜色类型
  896. this._selectbox = [];
  897. this._eliminatePos = [];//消除
  898. this._eliminateNum = 0;
  899. this._clicktipvec = [];
  900. this._candealwith = true;
  901. this._limitproptag = -1;
  902. this._clicktiptime = 0;
  903. this._selectprop = 0;
  904. this.propNode = [];
  905. this._propmotion = [];
  906. this._proptipsp = [];
  907. this._firstsavein = false;
  908. },
  909. smallPeopleCollect: function (vec) {
  910. var tt = cleardelaytime;
  911. var action = [];
  912. for (var i in vec) {
  913. var pos = vec[i];
  914. action.push(cc.callFunc(function (target, pos) {
  915. this.addParticle(pos, 2);
  916. this.removeBox(pos, 1);
  917. }.bind(this), this, pos));
  918. action.push(cc.delayTime(boxremovesingletime));
  919. tt += boxremovesingletime;
  920. }
  921. if (action.length > 0) {
  922. this.node.runAction(cc.sequence(action));
  923. }
  924. return tt;
  925. },
  926. addParticle: function (pos, key) {
  927. if (!key)
  928. key = 1;
  929. if (key == 1)
  930. this.addFkLizi(this.clipDraw, this.chessPos(pos.x, pos.y), this.getChessInfo(pos) - 1, 1.0, 2);
  931. else
  932. this.addFkLizi(this.clipDraw, this.chessPos(pos.x, pos.y), 5, 1.0, 2);
  933. },
  934. removeBox: function (pos, tag) {
  935. if (!tag) {
  936. tag = 2;
  937. }
  938. var node = this.getClipItem(pos.x * STAR_HEIGHT + pos.y);
  939. if (node && node.isValid) node.removeFromParent();
  940. this.setChessInfo(pos, 0);
  941. this.levelupmusic++;
  942. },
  943. /**
  944. * 星星粒子特效
  945. * @param node
  946. * @param pos
  947. * @param idx
  948. * @param scale
  949. * @param type
  950. */
  951. addFkLizi: function (node, pos, idx, scale, type) {
  952. if (!type)
  953. type = 1;
  954. if (!scale)
  955. scale = 1.0;
  956. // var spname=chesspng[idx];
  957. var spname = "";
  958. if (type == 2) {
  959. if (idx == 5)
  960. spname = "game/lizi/starpar2";
  961. else
  962. spname = "game/lizi/starpar" + (3 + idx);
  963. } else if (type == 3) {
  964. spname = "game/lizi/starpar1";
  965. }
  966. if (idx == 5) {
  967. this.blast(5, pos, zy.randNum(0, 4));
  968. cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 1); //消除个数
  969. } else {
  970. this.blast(5, pos, idx);
  971. }
  972. },
  973. blast(score, pos, index) {
  974. let node = Blast.pool.get();
  975. if (!node) {
  976. node = cc.instantiate(this.BLAST_PREFAB);
  977. }
  978. node.position = pos;
  979. node.getComponent(this.BLAST_PREFAB.name).run(score, index);
  980. node.zIndex = 100;
  981. // node.scale = this.node.scale;
  982. node.parent = this.clipDraw;
  983. },
  984. getRetainScore: function (number) {
  985. var score = 1500;
  986. for (var i = 1; i <= number; i++) {
  987. score -= 30 * i - 15;
  988. }
  989. if (score < 0)
  990. score = 0;
  991. return score;
  992. },
  993. boxSequence: function (vec, sx) {
  994. var k = vec.length;
  995. if (k > 0) {
  996. for (var i = 0; i < k; i++) {
  997. for (var j = k - 1; j > i; j--) {
  998. if (sx) {
  999. if (this.getnum(vec[j]) < this.getnum(vec[j - 1])) {
  1000. var temp = vec[j];
  1001. vec[j] = vec[j - 1];
  1002. vec[j - 1] = temp;
  1003. }
  1004. } else {
  1005. if (this.getrenum(vec[j]) < this.getrenum(vec[j - 1])) {
  1006. var temp = vec[j];
  1007. vec[j] = vec[j - 1];
  1008. vec[j - 1] = temp;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. },
  1015. getnum: function (pos) {
  1016. return pos.x * STAR_HEIGHT + pos.y;
  1017. },
  1018. getrenum: function (pos) {
  1019. return pos.x * STAR_HEIGHT + (STAR_HEIGHT - pos.y);
  1020. },
  1021. chessCanEliminate: function () {
  1022. for (var i = 0; i < STAR_WIDTH; i++)
  1023. for (var j = 0; j < STAR_HEIGHT; j++) {
  1024. if (this.singelCanEliminate(cc.v2(i, j))) {
  1025. return true;
  1026. }
  1027. }
  1028. return false;
  1029. },
  1030. chessMove: function () {
  1031. var temp = false;
  1032. var downstep = [];
  1033. var step = 0;
  1034. for (var i = 0; i < STAR_WIDTH; i++) {
  1035. step = 0;
  1036. for (var j = 0; j < STAR_HEIGHT; j++) {
  1037. if (this.getChessInfo(cc.v2(i, j)) == 0) {
  1038. step++;
  1039. } else if (step > 0) {
  1040. temp = true;
  1041. var box = this.getClipItem(i * STAR_HEIGHT + j);
  1042. if (box) {
  1043. box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.15, this.chessPos(i, j - step))));
  1044. // box.setTag(i*STAR_HEIGHT+(j-step));
  1045. box.name = this.getBoxTag(i * STAR_HEIGHT + (j - step));
  1046. if (i * STAR_HEIGHT + j == this._limitproptag) {
  1047. this._limitproptag = i * STAR_HEIGHT + (j - step);
  1048. }
  1049. this.setChessInfo(cc.v2(i, j - step), this.getChessInfo(cc.v2(i, j)));
  1050. this.setChessInfo(cc.v2(i, j), 0);
  1051. }
  1052. }
  1053. }
  1054. }
  1055. step = 0;
  1056. for (var i = 0; i < STAR_WIDTH; i++) {
  1057. if (this.getChessInfo(cc.v2(i, 0)) == 0) {
  1058. step++;
  1059. } else if (step > 0) {
  1060. for (var j = 0; j < STAR_HEIGHT; j++) {
  1061. if (this.getChessInfo(cc.v2(i, j))) {
  1062. temp = true;
  1063. var box = this.getClipItem(i * STAR_HEIGHT + j);
  1064. if (box) {
  1065. box.runAction(cc.sequence(new cc.delayTime(0.1), new cc.moveBy(0.05, cc.v2(-step * STAR_SIZE_WIDTH, 0))));
  1066. // box.setTag((i-step)*STAR_HEIGHT+j);
  1067. box.name = this.getBoxTag((i - step) * STAR_HEIGHT + j);
  1068. this.setChessInfo(cc.v2(i - step, j), this.getChessInfo(cc.v2(i, j)));
  1069. this.setChessInfo(cc.v2(i, j), 0);
  1070. if (i * STAR_HEIGHT + j == this._limitproptag) {
  1071. this._limitproptag = (i - step) * STAR_HEIGHT + j;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. }
  1078. return temp;
  1079. },
  1080. hessCanEliminate: function () {
  1081. for (var i = 0; i < STAR_WIDTH; i++)
  1082. for (var j = 0; j < STAR_HEIGHT; j++) {
  1083. if (this.singelCanEliminate(cc.v2(i, j))) {
  1084. return true;
  1085. }
  1086. }
  1087. return false;
  1088. },
  1089. singelCanEliminate: function (pos) {
  1090. var i = pos.x;
  1091. var j = pos.y;
  1092. var key = this.getChessInfo(pos);
  1093. if (key > 0) {
  1094. if ((i - 1) >= 0 && key == this.getChessInfo(cc.v2(i - 1, j))) {
  1095. return true;
  1096. }
  1097. if ((i + 1) < STAR_WIDTH && key == this.getChessInfo(cc.v2(i + 1, j))) {
  1098. return true;
  1099. }
  1100. if ((j - 1) >= 0 && key == this.getChessInfo(cc.v2(i, j - 1))) {
  1101. return true;
  1102. }
  1103. if ((j + 1) < STAR_HEIGHT && key == this.getChessInfo(cc.v2(i, j + 1))) {
  1104. return true;
  1105. }
  1106. }
  1107. return false;
  1108. },
  1109. setChessInfo: function (pos, key) {
  1110. this._chessInfo[pos.x * STAR_HEIGHT + pos.y] = key;
  1111. },
  1112. getChessInfo: function (pos) {
  1113. return this._chessInfo[pos.x * STAR_HEIGHT + pos.y];
  1114. },
  1115. onTouchMoved: function (event) {
  1116. cc.log("onTouchMoved");
  1117. },
  1118. onTouchEnded: function (event) {
  1119. cc.log("onTouchEnded");
  1120. },
  1121. resurrenction: function(){
  1122. this.setScore(this._lastLevelScore);
  1123. this.node.runAction(new cc.Sequence(new cc.DelayTime(0.8),new cc.CallFunc(function () {
  1124. this.starDrop();
  1125. }.bind(this))))
  1126. },
  1127. // update (dt) {},
  1128. onDestroy() {
  1129. //关闭所有音乐、音效
  1130. cc.audioEngine.stopAll();
  1131. //移除所有事件监听
  1132. cc.game.targetOff(this);
  1133. }
  1134. });