/** * 游戏核心部分 */ let GameEvent = require('GameEvent'); var ZYGameTool = require('GameTool'); let Blast = require('Blast'); cc.Class({ extends: require("ZY_GameLayer"), properties: { touchLayer: cc.Node,//触摸Layer clipDraw: cc.Node,//棋盘绘制节点 clipDrawTemp: cc.Node,//棋盘绘制节点 propNode: { tooltip: '道具', type: cc.Node, default: [], }, leveNumLabel: cc.Label,//当前关卡 targetScoreLabel: cc.Label,//目标分数 scoreLabel: cc.Label,//当前分数 coinNumLabel: cc.Label,//当前分数 propTipNode: cc.Node, pauseBtn: cc.Node, gameEndLayer: cc.Prefab, gamePauseLayer: cc.Prefab, labelTip: cc.Prefab, maskLayer: cc.Prefab, bombSpine: cc.Prefab, scoreNum: cc.Prefab, helpNode: cc.Prefab, BLAST_PREFAB: { tooltip: '爆炸特效预制体', type: cc.Prefab, default: null, }, topBg: cc.Node, }, onLoad() { this.node.getChildByName("chessBg").scale = 0; }, start() { this.setGameState(GameState.STATE_STOP); zy.stopBgMusic(); this.clipDraw.active = false; this.schedule(this.dataUpdate, 0.1); this.init(); this.initChess(); this.initData(); this.initTouchLayer(); // this.retainScoreMotion(8, 10, 3.0); //奖励多少分 this.node.runAction(cc.sequence(new cc.delayTime(0.9), cc.callFunc(function () { this.starDrop(); }.bind(this)))); // this.gameFaild(); //游戏顶部全面屏适配 this.node.runAction(cc.sequence(cc.delayTime(0.01), cc.callFunc(function () { if (WINSIZE.width / WINSIZE.height < 0.53) { this.topBg.parent.y -= 60; this.node.getChildByName("btnNode").y -= 60; } }.bind(this)))); cc.game.on(cc.game.EVENT_HIDE, function () { console.log("游戏进入后台"); gameLocalData.save(this); // this.onHideGame();//处理游戏切到后台时的事件 }, this); cc.game.on(cc.game.EVENT_SHOW, function () { console.log("重新返回游戏"); // this.onShowGame();//处理游戏切回前台时的事件 }, this); }, /** * 更新Label数据 */ dataUpdate: function () { this.leveNumLabel.string = this.getLevel().toString(); this.targetScoreLabel.string = this.getTarget().toString(); this.scoreLabel.string = this.getScore().toString(); this.coinNumLabel.string = ZYGameTool.getInstance().getCoin().toString(); }, init: function () { this.BANER_HEIGHT = 150; heightoffest = this.BANER_HEIGHT + 10; console.log("--coinNum--: ", coinNum); this.coinNumLabel.string = coinNum.toString(); this._guide = ZYGameTool.getInstance().getIntegerForKey(localDataItem.gameGuide, 1); this._guide = this._guide === 1 ? true : false; this._guidenumber=-1; if (this._guide){ this.showHelpLayer(); } this.initBtn(); //棋盘背景动画 this.node.getChildByName("chessBg").scaleY = 1; this.node.getChildByName("chessBg").runAction(cc.sequence(cc.delayTime(0.3), cc.scaleTo(0.5, 1))); this.initProp(); }, showHelpLayer:function(){ var helpNode = cc.instantiate(this.helpNode); helpNode.parent = this.node; helpNode.zIndex = 1000; ZYGameTool.getInstance().setIntegerForKey(localDataItem.gameGuide, 0); Module.GameHelpLayer.setParam(false); }, initBtn: function () { //暂停按钮 this.pauseBtn.on('click', function (event) { this.pauseClick(); }.bind(this)); }, pauseClick: function () { var pauseLayer = cc.instantiate(this.gamePauseLayer); pauseLayer.parent = this.node; pauseLayer.zIndex = 1000; this.node.runAction(cc.sequence(cc.delayTime(0.1), cc.callFunc(function () { gameLocalData.save(this); }.bind(this)))); Module.GamePauseLayer.call(function (name) { if (name === "backGameBtn") { } else if (name === "restGameBtn") { this.clearData(); gameLocalData.clear(); this.clipDraw.removeAllChildren(); this.initData(); this.node.runAction(cc.sequence(new cc.delayTime(0.3), cc.callFunc(function () { this.starDrop(); }.bind(this)))); } else if (name === "backHomeBtn") { cc.director.loadScene("homeScene"); } }.bind(this)); }, initProp: function () { // this.propTipNode.active = true; for (var i = 0; i < 2; i++) { this._propNode[i] = this.propNode[i]; this._propmotion[i] = this.propNode[i].getChildByName("effect"); this._proptipsp[i] = this.propTipNode.getChildByName("propTip" + (i + 1)); } }, initChess: function () { this.node.getChildByName("chessBg").y += heightoffest - 10; this.clipDraw.active = true; }, /** * 恢复上次存档 */ setLastData: function () { gameLocalData.getSaveData(); var lastdata = gameLocalData; if (lastdata._issaved) { this._firstsavein = true; this.setLevel(lastdata._level); this.setScore(lastdata._score); this.setTarget(lastdata._target); this._lastLevelScore = lastdata._lastscore; var chessinfo = lastdata._chesspad; console.log("--chessinfo--: ", chessinfo); for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { if (parseInt(chessinfo[i * STAR_HEIGHT + j]) > 0) { this._chessInfo[i * STAR_HEIGHT + j] = parseInt(chessinfo[i * STAR_HEIGHT + j]); } } this.node.runAction(cc.sequence(cc.delayTime(1.5) , cc.callFunc(function () { this.handleHandle(); }.bind(this)))); } }, /** * 初始化数据信息 */ initData: function () { this._candealwith = true; this.setLevel(1); this.setScore(0); this.setTarget(this.getTargetScoreByRound(1)); this._lastLevelScore = 0; for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) this._chessInfo[i * STAR_HEIGHT + j] = 0; this._selectbox = []; this._eliminateNum = 0; this._eliminatePos = []; this.setLastData(); }, /** * 分数算法 * @param level * @returns {number} */ getTargetScoreByRound: function (level) { var targetscore = 0; if (level == 1) targetscore = 1000; else targetscore = 10 * level * level + 1950 * level - 1440; return targetscore; }, /** * 设置游戏状态 * @param state */ setGameState: function (state) { this._gamestate = state; }, /** * 生成棋盘 */ starDrop: function () { this.setGameState(GameState.STATE_STOP); this._max = this.getTarget() - this.score; cc.game.emit(GameEvent.EventType.GAME_PROGRESS, 0); //加限时道具 if (!this._firstsavein ) { for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { var color = zy.randNum(1, 5); this._chessInfo[i * STAR_HEIGHT + j] = color; } } var time = this.chessAppear(6);//zy.randNum(1, 6) console.log("time: " , time); this.node.runAction(new cc.sequence(new cc.delayTime(time), new cc.callFunc(function () { console.log("--this.getGameState-1: ", this.getGameState()); this.setGameState(GameState.STATE_RUN); }.bind(this)))); }, /** * 棋盘出现特效 * @param val 特效类型 * @returns {number} */ chessAppear: function (val) { 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]; for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { var id = this._chessInfo[i * STAR_HEIGHT + j]; if (id > 0) { var box = zy.getSprite("game/star/starchess" + (id) + ".png"); box.setScale(STAR_CHESS_SCALE); // this.clipDraw.addChild(box,CHESSMAN_LEVEL,(i*STAR_HEIGHT+j)+""); this.clipDraw.addChild(box, CHESSMAN_LEVEL, this.getBoxTag((i * STAR_HEIGHT + j))); var pos = this.chessPos(i, j); box.setPosition(cc.v2(pos.x, pos.y + STAR_HEIGHT * STAR_SIZE_WIDTH)); // if((i*STAR_HEIGHT+j)==this._limitproptag){ // box.setTexture(url_xmtg+ "res/animation/starpng/block_"+id+"_lb.png"); // } if (val == 0) { box.setPosition(cc.v2(pos.x, pos.y + STAR_HEIGHT * STAR_SIZE_WIDTH)); 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)))); } else if (val == 1) { box.setPosition(cc.v2(pos.x - STAR_WIDTH * STAR_SIZE_WIDTH, pos.y)); box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos))); } else if (val == 2) { box.setPosition(cc.v2(pos.x + STAR_WIDTH * STAR_SIZE_WIDTH, pos.y)); box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos))); } else if (val == 3) { box.setPosition(pos); box.opacity = 0; box.runAction(cc.sequence(cc.delayTime(0), cc.fadeIn(1.0))); } else if (val == 4) { if (i < STAR_WIDTH / 2.0) { box.setPosition(cc.v2(pos.x - (STAR_WIDTH * STAR_SIZE_WIDTH / 2.0), pos.y)); } else { box.setPosition(cc.v2(pos.x + (STAR_WIDTH * STAR_SIZE_WIDTH / 2.0), pos.y)); } box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos))); } else if (val == 5) { if (j < STAR_HEIGHT / 2.0) { box.setPosition(cc.v2(pos.x, pos.y - (STAR_HEIGHT * STAR_SIZE_WIDTH / 2.0))); } else { box.setPosition(cc.v2(pos.x, pos.y + (STAR_HEIGHT * STAR_SIZE_WIDTH / 2.0))); } box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.4, pos))); } else if (val == 6) { var k = 1; if ((j % 2) == 0) k = -1; box.setPosition(cc.v2(pos.x + k * STAR_WIDTH * STAR_SIZE_WIDTH, pos.y)); box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.5, pos))); } } } return time[val]; }, chessPos: function (i, j) { return cc.v2(WINSIZE.width / 2.0 + (i + 0.5 - STAR_WIDTH / 2.0) * STAR_SIZE_WIDTH, heightoffest + (j + 0.5) * STAR_SIZE_WIDTH); }, /** * 获取游戏状态 * @returns {number} */ getGameState: function () { return this._gamestate; }, /** * 初始化监听层 */ initTouchLayer: function () { this.touchLayer.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan.bind(this), this); this.touchLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMoved.bind(this), this); this.touchLayer.on(cc.Node.EventType.TOUCH_END, this.onTouchEnded.bind(this), this); this.touchLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnded.bind(this), this); }, txtTip: function (str) { var labelTip = cc.instantiate(this.labelTip); labelTip.parent = this.node; labelTip.scale = 0.7; labelTip.setPosition(cc.v2(0, 0)); labelTip.getComponent(cc.Label).string = str.toString(); labelTip.runAction(cc.sequence(cc.moveBy(0.5, cc.v2(0, WINSIZE.height / 2 - 50)), cc.delayTime(1.2), cc.removeSelf(true))); return labelTip; }, /** * 开始监听 * @param event * @returns {boolean} */ onTouchBegan: function (event) { cc.log("onTouchBegan"); console.log("--this.getGameState-2: ", this.getGameState()); var position = event.getLocation(); var target = this; if (target.getGameState() != GameState.STATE_RUN) { return false; } target._firstsavein = false; var k = target.propTouch(position); if (k > 0 && target._eliminateNum == 0) { if (PROPPRICE[k - 1] > coinNum) { this._selectprop = 0; this.txtTip("金币不足"); return false; } target.propMotion(k); return true; } if (position.x >= 0 && position.x < WINSIZE.width && position.y >= heightoffest && position.y <= (heightoffest + STAR_SIZE_WIDTH * STAR_HEIGHT)) { var pos = target.chessPt(position); if (target._chessInfo[pos.x * STAR_HEIGHT + pos.y] && ZYGameTool.getInstance().posIndexOf(target._eliminatePos, pos) == -1) { // if (target._selectprop == 2) target._selectprop =4 ; var temp = true; var vec = []; if (target._selectprop == 1 || target._selectprop == 2) { temp = target.propProcess(target._selectprop - 1, pos); // if(target._guide){ // target.removeChildByTag(TIPFONTTAG); // target._guidenumber++; // } } else if (target._selectprop == 0) { target.boxTip(pos, vec); } var selectnumber = vec.length; if (selectnumber > 1 || (target._selectprop > 0 && temp)) { target.endTip(); var ft = 0.0; if (target._selectprop > 0) { // ELSGameLogic.getInstance().gameCount("item","prop_"+target._selectprop); } if (target._selectprop == 0) { target._eliminateNum++; target.boxEliminate(vec); ft = boxremovesingletime * selectnumber + cleardelaytime; } else if (target._selectprop == 1) { ft = boxremovesingletime + 0.9; } else if (target._selectprop == 2) { ft = target._selectbox.length * 0.15; target.useProps4(pos); } if (target._selectprop == 1 || target._selectprop == 2) { target.setGameState(GameState.STATE_STOP); target.node.runAction(cc.sequence(cc.delayTime(ft), cc.callFunc(function () { target.touchHandle(); }.bind(target)))); target._selectprop = 0; target._selectbox = []; } } } return true; } return false; }, //获取道具坐标 propTouch: function (pos) { for (var i = 0; i < 2; i++) { if (this._propNode[i].getBoundingBoxToWorld().contains(pos)) return i + 1; } return 0; }, //获取道具位置 getPropPos: function (key) { if (key > 0 && key < 2) { return this._propNode[key - 1].parent.convertToWorldSpaceAR(this._propNode[key - 1].getPosition()); } return cc.v2(0, 0); }, useProps4: function (pos) { var maskLayer = cc.instantiate(this.maskLayer); maskLayer.zIndex = 99; maskLayer.parent = this.node; this.clipDrawTemp.zIndex = 100; var tempk = this._selectbox[0]; for (var i in this._selectbox) { if (this._selectbox[i].x == pos.x && this._selectbox[i].y == pos.y) { this._selectbox[i].x = tempk.x; this._selectbox[i].y = tempk.y; this._selectbox[0].x = pos.x; this._selectbox[0].y = pos.y; break; } } var selectbox = this._selectbox.slice(1, this._selectbox.length); selectbox.sort(function (a, b) { return Math.random() > 0.5 ? -1 : 1; }); selectbox.splice(0, 0, this._selectbox[0]); var actions = []; for (var i = 0; i < selectbox.length; i++) { var key1 = selectbox[i]; var node = this.getClipItem(key1.x * STAR_HEIGHT + key1.y); node.index = 101; actions.push(cc.callFunc(function (target, key) { this.addParticle(key); this.removeBox(key); }.bind(this), this, key1)); if (i == selectbox.length - 1) { break; } var key2 = selectbox[i + 1]; var position1 = this.chessPos(key1.x, key1.y); var position2 = this.chessPos(key2.x, key2.y); actions.push(cc.callFunc(function (target, data) { // var line=new cc.Sprite(starpng[wb.randNum(14,18)]); var line = zy.getSprite("game/prop/starprope" + zy.randNum(1, 5)); line.setAnchorPoint(cc.v2(0, 0.5)); 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)); var wid = 70; var lengthscale = length * 1.0 / wid; line.scaleX = 0.06 * lengthscale; cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 1); //消除个数 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 () { line.removeFromParent(); }))); line.setPosition(data.pos1); var angle1 = Math.atan2(data.pos2.y - data.pos1.y, data.pos2.x - data.pos1.x); var angle = -1 * cc.misc.radiansToDegrees(angle1); line.setRotation(angle); this.clipDrawTemp.addChild(line, 102); var score = this.getScoreByNum(1); this.updateScore(score); }.bind(this), this, {pos1: position1, pos2: position2})); actions.push(cc.delayTime(0.15)); } actions.push(cc.callFunc(function () { maskLayer.removeFromParent(); this.clipDrawTemp.removeAllChildren(true); this.showPropTip(0); }.bind(this))); this.node.runAction(cc.sequence(actions)); }, //使用道具 propProcess: function (id, pos, key) { if (!pos) { pos = cc.v2(0, 0); } if (!key) { key = 1; } if (this._selectprop > 0) { this.showPropMotion(this._selectprop, 0); } // if (key == 1) { console.log("id: ", id); if (PROPPRICE[id] > coinNum) { this._selectprop = 0; this.txtTip(); return false; } else { this.txtTip("使用道具,扣除金币 -" + PROPPRICE[id]); ZYGameTool.getInstance().useCoin(-PROPPRICE[id]); } } if (id == 1) { var key = this.getChessInfo(pos); console.log("---key--: ", key); for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { if (key == this.getChessInfo(cc.v2(i, j))) { this._selectbox.push(cc.v2(i, j)); } } } else if (id == 0) { // if(id==0){ var ssp = zy.getSprite("game/prop/prop1"); this.clipDraw.addChild(ssp, 1000); console.log("--this.getPropPos(1)--: ", this.getPropPos(1)); ssp.setPosition(this.getPropPos(1)); var v1 = ssp.getPosition(); var tempv = this.chessPos(pos.x, pos.y); var v2 = cc.v2(tempv.x + 10, tempv.y - 10); var v3 = cc.v2(0, 0); if (v2.x < WINSIZE.width / 2) v3 = cc.v2(v1.x + 200, v1.y - 100); else v3 = cc.v2(v1.x + 200, v1.y + 100); var bezierpath = [v1, v3, v2]; var bezier = cc.bezierTo(0.7, bezierpath); var bombSpine = this.createBombSpine(tempv, this.clipDraw); bombSpine.active = false; var bombAction = cc.callFunc(function () { bombSpine.active = true; }); cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 6); //消除个数 ssp.runAction(cc.sequence(bezier, bombAction, cc.delayTime(0.2), cc.callFunc(function (target, tempv) { bombSpine.removeFromParent(); ssp.removeFromParent(); }.bind(this), this, tempv))); ssp.runAction(cc.scaleTo(0.15, 1.5)); this.node.runAction(cc.sequence(cc.delayTime(0.7), cc.callFunc(function (target, pos) { this.node.runAction(cc.sequence(cc.delayTime(0.0), cc.callFunc(function (target, pos) { var temp = 0; for (var i = pos.x - 1; i <= pos.x + 1; i++){ for (var j = pos.y - 1; j <= pos.y + 1; j++) { if (i >= 0 && i < STAR_WIDTH && j >= 0 && j < STAR_HEIGHT && this.getChessInfo(cc.v2(i, j))) { this.addParticle(cc.v2(i, j)); this.removeBox(cc.v2(i, j)); ++temp; } } } var score = this.getScoreByNum(temp); this.updateScore(score); }.bind(this), this, pos))); this.showPropTip(0); }.bind(this), this, pos))); } return true; }, createBombSpine: function (pos, node, index) { if (index == undefined) index = 10000; var bombSpine = cc.instantiate(this.bombSpine); bombSpine.parent = node; bombSpine.zIndex = index; bombSpine.setPosition(pos); return bombSpine; }, propMotion: function (k) { if (this._selectprop > 0) { this.showPropMotion(this._selectprop, 0); } cc.log("match touch began2", k); if (this._selectprop == k) { this._selectprop = 0; this.showPropTip(0); } else { this._selectprop = k; this.showPropTip(k); this.showPropMotion(k, 1); } }, showPropTip: function (k) { if (k == 0) { this.propTipNode.active = false; } else if (k > 0 && k < 3) { this.propTipNode.active = true; for (var i = 0; i < 2; i++) { if (i == (k - 1)) { console.log("--showPropTip--"); this._proptipsp[i].active = true; } else { this._proptipsp[i].active = false; } } } }, //道具点击动画 showPropMotion: function (key, type) { if (type == 1) { this._propmotion[key - 1].active = true; this._propmotion[key - 1].runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.0, 1.0), cc.scaleTo(1.0, 1.5)))); this._propmotion[key - 1].runAction(cc.repeatForever(cc.sequence(cc.fadeIn(0.0), cc.fadeOut(1.0)))); } else if (type == 0) { this._propmotion[key - 1].active = false; this._propmotion[key - 1].stopAllActions(); this._propmotion[key - 1].setScale(1.0); this._propmotion[key - 1].opacity = 255; } }, getScoreByNum: function (num) { var k = 5; var t = 5; for (var i = 0; i < num; i++) { k += t; t += 10; } return k; }, updateScore: function (score) { this.score += score; }, scoreMotion: function (score, pt) { var scoreNum = cc.instantiate(this.scoreNum); scoreNum.parent = this.clipDraw; scoreNum.zIndex = 10000; scoreNum.getComponent(cc.Label).string = score.toString(); scoreNum.setPosition(this.chessPos(pt.x, pt.y)); scoreNum.runAction(cc.sequence(cc.moveBy(1.0, cc.v2(0, STAR_SIZE_WIDTH)), cc.callFunc(function () { scoreNum.removeFromParent(); }))); }, /** * 连消提示 * @param vec */ boxEliminate: function (vec) { var score = this.getScoreByNum(vec.length); this.updateScore(score); console.log("--score--: ", score); this.scoreMotion(score, vec[0]); if (this.getScore() >= this.getTarget() && (this.getScore() - score) < this.getTarget()) { this.spriteShow(zy_gxpng[0]); } else { if (vec.length == 5) { this.spriteShow("game/main_tip/great"); } else if (vec.length > 5) { this.spriteShow("game/main_tip/zan"); } } this.levelupmusic = 0; this.boxSequence(vec, true); var arrayOfActions = []; for (var i in vec) { if (i != "0") { arrayOfActions.push(cc.delayTime(boxremovesingletime)); } var key = vec[i]; arrayOfActions.push(cc.callFunc(function (target, data) { this.addParticle(data); this.removeBox(data); }.bind(this), this, key)); } arrayOfActions.push(cc.delayTime(cleardelaytime)); arrayOfActions.push(cc.callFunc(function () { this._eliminateNum--; if (this._eliminateNum == 0) { this._eliminatePos = []; this.setGameState(GameState.STATE_STOP); this.touchHandle(); } }.bind(this))); this.node.runAction(cc.sequence(arrayOfActions)); }, spriteShow: function (str) { if (str == zy_gxpng[0]) { console.log("---chaoyue"); var endpos = cc.v2(0, WINSIZE.height * Hkey - 180); var spr = zy.getSprite(str); this.node.addChild(spr, 1000, "777"); spr.setPosition(cc.v2(0, 0 + heightoffest / 2.0)); 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), cc.scaleTo(0.2, 0.8), cc.delayTime(0.52), cc.moveTo(0.5, endpos))); spr.runAction(cc.sequence(new cc.delayTime(1.2), cc.scaleTo(0.5, 0.25), cc.callFunc(function () { spr.removeFromParent(); var sp = zy.getSprite(str); sp.setScale(0.25); // this.getParent().getBackLayer().getChildByName("node").addChild(sp,10,777); this.node.addChild(spr, 12, "777"); sp.setPosition(cc.v2(-170, -175)); }.bind(this)))); } else { this.spriteShow2(str, cc.v2(0, 0)); } }, spriteShow2: function (res, pos) { var sp = zy.getSprite(res); this.node.addChild(sp, 1000); sp.setPosition(pos); sp.setScale(1.0); if (res == "game/main_tip/xsyd") { sp.runAction(sequence(cc.moveBy(0.5, cc.v2(0, 50)), cc.delayTime(0.2), cc.fadeOut(0.3), cc.callFunc(function () { sp.removeFromParent(); }))); } else { 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), cc.scaleTo(0.2, 0.8), cc.delayTime(0.2), cc.fadeOut(0.3), cc.callFunc(function () { sp.removeFromParent(); }))); } }, /** * 可以消除的元素放入_eliminatePos * @param pos * @param vect */ boxTip: function (pos, vect) { var vec = this.boxGet(pos); if (vec.length > 1) { for (var i in vec) { vect.push(vec[i]); this._eliminatePos.push(vec[i]); var box = this.getClipItem(vec[i].x * STAR_HEIGHT + vec[i].y); this.setBoxMotion(box, true); } } cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, vect.length); //消除个数 }, getClipItem: function (tag) { return this.clipDraw.getChildByName(tag.toString()); }, //TAG值转换为String类型 getBoxTag: function getBoxTag(v) { return v.toString(); }, /** * 消除结束 */ endTip: function () { this._clicktiptime = 0; for (var i in this._clicktipvec) { var key = this._clicktipvec[i]; var box = this.getClipItem(key.x * STAR_HEIGHT + key.y); this.setBoxMotion(box, false); } this._clicktipvec = []; }, /** * 获取可以消除的元素 * @param pos * @returns {[]} */ boxGet: function (pos) { var vec = []; this.boxAdd(pos, vec); return vec; }, /** * 树状搜索消除元素 * @param pos * @param vec */ boxAdd: function (pos, vec) { vec.push(pos); //x-1 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)))) { this.boxAdd(cc.v2(pos.x - 1, pos.y), vec); } //x+1 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)))) { this.boxAdd(cc.v2(pos.x + 1, pos.y), vec); } //y-1 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)))) { this.boxAdd(cc.v2(pos.x, pos.y - 1), vec); } //y+1 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)))) { this.boxAdd(cc.v2(pos.x, pos.y + 1), vec); } }, /** * 星星消除给个动作 * @param box * @param b */ setBoxMotion: function (box, b) { if (box) { if (b) { box.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(1.0, 0.89), cc.scaleTo(1.0, 1.0)))); } else { box.stopAllActions(); box.setScale(1.0); } } }, /** * 通过坐标找到目标对象 * @param pos * @returns {cc.Vec2} */ chessPt: function (pos) { var x = parseInt((pos.x - WINSIZE.width / 2.0) / STAR_SIZE_WIDTH + STAR_WIDTH / 2.0); var y = parseInt((pos.y - heightoffest) / STAR_SIZE_WIDTH); return cc.v2(x, y); }, touchHandle: function () { var time = 0.0; if (this.chessMove()) { time += chessmovetime; } this.node.runAction(cc.sequence(cc.delayTime(time), cc.callFunc(function () { if (this._candealwith) { this.handleHandle(); } }.bind(this)))); }, handleHandle: function () { if (this.chessCanEliminate()) { console.log("--handleHandle--: "); this.setGameState(GameState.STATE_RUN); } else { this.node.runAction(cc.sequence(cc.delayTime(0.5), new cc.callFunc(function () { this.chessClear(); }.bind(this)))) } }, chessClear: function () { var eliminatebox = []; for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { if (this.getChessInfo(cc.v2(i, j))) { eliminatebox.push(cc.v2(i, j)); } } if (eliminatebox.length > 0) { eliminatebox.sort(function (a, b) { return Math.random() > 0.5 ? -1 : 1; }) } this.levelupmusic = 0; this.boxSequence(eliminatebox, false); var num = this.getRetainScore(eliminatebox.length); this.updateScore(num); // ELSGameData.getInstance().setDayHighScore(this._score,num); this.scoreMotion(num, cc.v2(WINSIZE.width / 2, WINSIZE.height / 2)); // ELSGameLogic.getInstance().showHyRank(this._score,false); var ftime = this.smallPeopleCollect(eliminatebox); this.retainScoreMotion(eliminatebox.length, num, 3.0); //奖励多少分 var ft = Math.max(ftime, 3); if (this.getScore() > this.getTarget() && (this.getScore() - num) < this.getTarget()) { this.spriteShow(zy_gxpng[0]); } this.node.runAction(cc.sequence(cc.delayTime(ft), new cc.callFunc(function () { this.gameEnd(); }.bind(this)))); }, retainScoreMotion: function (number, score, ft) { var fnt1 = cc.instantiate(this.labelTip); fnt1.zIndex = 100; fnt1.parent = this.node; fnt1.getComponent(cc.Label).string = "奖励" + score + "分"; fnt1.x = -(WINSIZE.width / 2 + 100); fnt1.y = heightoffest * 0.5; fnt1.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.2, cc.v2(0, heightoffest * 0.5)))); var fnt2 = cc.instantiate(this.labelTip); fnt2.zIndex = 100; fnt2.parent = this.node; fnt2.getComponent(cc.Label).string = "剩余" + number + "颗糖果"; fnt2.x = -(WINSIZE.width / 2 + 100); fnt2.y = heightoffest * 0.5 - 100; 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 () { fnt1.removeFromParent(); fnt2.removeFromParent(); }))); }, /** * 游戏结束 */ gameEnd: function () { cc.game.emit(GameEvent.EventType.GAME_PASS_STAGE); //消除个数 //闯关失败 if (this.getScore() < this.getTarget()) { this.clearData(); gameLocalData.clear(); cc.director.loadScene("homeScene"); } else { //闯关成功 // this.LevelUpMotion(); if (this.getScore() >= this.getTarget()) { this.spriteShow(zy_gxpng[0]); } this.setLevel(this.getLevel() + 1); this.setTarget(this.getTargetScoreByRound(this.getLevel())); this._lastLevelScore = this.getScore(); this.node.runAction(cc.sequence(cc.delayTime(0.8), cc.callFunc(function () { this._firstsavein = false; this.starDrop(); }.bind(this)))); } }, clearData: function () { this.score = 0; this._lastLevelScore = 0; this.level = 1; this.target = 0; this.hang = 0; this.chessInfo = [];//记录棋盘元素的颜色类型 this._selectbox = []; this._eliminatePos = [];//消除 this._eliminateNum = 0; this._clicktipvec = []; this._candealwith = true; this._limitproptag = -1; this._clicktiptime = 0; this._selectprop = 0; this.propNode = []; this._propmotion = []; this._proptipsp = []; }, gameFaild: function () { var gameFaildNode = cc.instantiate(this.gameEndLayer); gameFaildNode.zIndex = 100; gameFaildNode.parent = this.node; Module.GameFaildLayer.call(function (state) { }.bind(this)); }, smallPeopleCollect: function (vec) { var tt = cleardelaytime; var action = []; for (var i in vec) { var pos = vec[i]; action.push(cc.callFunc(function (target, pos) { this.addParticle(pos, 2); this.removeBox(pos, 1); }.bind(this), this, pos)); action.push(cc.delayTime(boxremovesingletime)); tt += boxremovesingletime; } if (action.length > 0) { this.node.runAction(cc.sequence(action)); } return tt; }, addParticle: function (pos, key) { if (!key) key = 1; if (key == 1) this.addFkLizi(this.clipDraw, this.chessPos(pos.x, pos.y), this.getChessInfo(pos) - 1, 1.0, 2); else this.addFkLizi(this.clipDraw, this.chessPos(pos.x, pos.y), 5, 1.0, 2); }, removeBox: function (pos, tag) { if (!tag) { tag = 2; } var node = this.getClipItem(pos.x * STAR_HEIGHT + pos.y); if (node && node.isValid) node.removeFromParent(); this.setChessInfo(pos, 0); this.levelupmusic++; }, /** * 星星粒子特效 * @param node * @param pos * @param idx * @param scale * @param type */ addFkLizi: function (node, pos, idx, scale, type) { if (!type) type = 1; if (!scale) scale = 1.0; // var spname=chesspng[idx]; var spname = ""; if (type == 2) { if (idx == 5) spname = "game/lizi/starpar2"; else spname = "game/lizi/starpar" + (3 + idx); } else if (type == 3) { spname = "game/lizi/starpar1"; } if (idx == 5) { this.blast(5, pos, zy.randNum(0, 4)); cc.game.emit(GameEvent.EventType.GAME_CLEAR_COUNT, 1); //消除个数 } else { this.blast(5, pos, idx); } }, blast(score, pos, index) { let node = Blast.pool.get(); if (!node) { node = cc.instantiate(this.BLAST_PREFAB); } node.position = pos; node.getComponent(this.BLAST_PREFAB.name).run(score, index); node.zIndex = 100; // node.scale = this.node.scale; node.parent = this.clipDraw; }, getRetainScore: function (number) { var score = 1500; for (var i = 1; i <= number; i++) { score -= 30 * i - 15; } if (score < 0) score = 0; return score; }, boxSequence: function (vec, sx) { var k = vec.length; if (k > 0) { for (var i = 0; i < k; i++) { for (var j = k - 1; j > i; j--) { if (sx) { if (this.getnum(vec[j]) < this.getnum(vec[j - 1])) { var temp = vec[j]; vec[j] = vec[j - 1]; vec[j - 1] = temp; } } else { if (this.getrenum(vec[j]) < this.getrenum(vec[j - 1])) { var temp = vec[j]; vec[j] = vec[j - 1]; vec[j - 1] = temp; } } } } } }, getnum: function (pos) { return pos.x * STAR_HEIGHT + pos.y; }, getrenum: function (pos) { return pos.x * STAR_HEIGHT + (STAR_HEIGHT - pos.y); }, chessCanEliminate: function () { for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { if (this.singelCanEliminate(cc.v2(i, j))) { return true; } } return false; }, chessMove: function () { var temp = false; var downstep = []; var step = 0; for (var i = 0; i < STAR_WIDTH; i++) { step = 0; for (var j = 0; j < STAR_HEIGHT; j++) { if (this.getChessInfo(cc.v2(i, j)) == 0) { step++; } else if (step > 0) { temp = true; var box = this.getClipItem(i * STAR_HEIGHT + j); if (box) { box.runAction(cc.sequence(cc.delayTime(0), cc.moveTo(0.15, this.chessPos(i, j - step)))); // box.setTag(i*STAR_HEIGHT+(j-step)); box.name = this.getBoxTag(i * STAR_HEIGHT + (j - step)); if (i * STAR_HEIGHT + j == this._limitproptag) { this._limitproptag = i * STAR_HEIGHT + (j - step); } this.setChessInfo(cc.v2(i, j - step), this.getChessInfo(cc.v2(i, j))); this.setChessInfo(cc.v2(i, j), 0); } } } } step = 0; for (var i = 0; i < STAR_WIDTH; i++) { if (this.getChessInfo(cc.v2(i, 0)) == 0) { step++; } else if (step > 0) { for (var j = 0; j < STAR_HEIGHT; j++) { if (this.getChessInfo(cc.v2(i, j))) { temp = true; var box = this.getClipItem(i * STAR_HEIGHT + j); if (box) { box.runAction(cc.sequence(new cc.delayTime(0.1), new cc.moveBy(0.05, cc.v2(-step * STAR_SIZE_WIDTH, 0)))); // box.setTag((i-step)*STAR_HEIGHT+j); box.name = this.getBoxTag((i - step) * STAR_HEIGHT + j); this.setChessInfo(cc.v2(i - step, j), this.getChessInfo(cc.v2(i, j))); this.setChessInfo(cc.v2(i, j), 0); if (i * STAR_HEIGHT + j == this._limitproptag) { this._limitproptag = (i - step) * STAR_HEIGHT + j; } } } } } } return temp; }, hessCanEliminate: function () { for (var i = 0; i < STAR_WIDTH; i++) for (var j = 0; j < STAR_HEIGHT; j++) { if (this.singelCanEliminate(cc.v2(i, j))) { return true; } } return false; }, singelCanEliminate: function (pos) { var i = pos.x; var j = pos.y; var key = this.getChessInfo(pos); if (key > 0) { if ((i - 1) >= 0 && key == this.getChessInfo(cc.v2(i - 1, j))) { return true; } if ((i + 1) < STAR_WIDTH && key == this.getChessInfo(cc.v2(i + 1, j))) { return true; } if ((j - 1) >= 0 && key == this.getChessInfo(cc.v2(i, j - 1))) { return true; } if ((j + 1) < STAR_HEIGHT && key == this.getChessInfo(cc.v2(i, j + 1))) { return true; } } return false; }, setChessInfo: function (pos, key) { this._chessInfo[pos.x * STAR_HEIGHT + pos.y] = key; }, getChessInfo: function (pos) { return this._chessInfo[pos.x * STAR_HEIGHT + pos.y]; }, onTouchMoved: function (event) { cc.log("onTouchMoved"); }, onTouchEnded: function (event) { cc.log("onTouchEnded"); }, // update (dt) {}, onDestroy() { //关闭所有音乐、音效 cc.audioEngine.stopAll(); //移除所有事件监听 cc.game.targetOff(this); } });