Cocos2d JS 之消灭星星(四) 游戏主场景顶部显示

 
  
 
   
  1 /*
  2  * 游戏主场景顶部显示信息
  3  */
  4 var GAMETOP;
  5 var GameTopInformation = ccui.Layout.extend(
  6 {
  7     size:null,
  8     isPause:false,//是否暂停游戏
  9     maxScoreLabel:null,//最高纪录
 10     getScoreNum:null,//当前得分
 11     currentLevel:null,//当前关卡
 12     isPassed:false,//是否通过关卡
 13     ctor:function()
 14     {
 15         this._super();
 16         this.zinit();
 17         this.setInformation();
 18     },
 19     //信息设置
 20     setInformation:function()
 21     {
 22         var maxRecord = new myImage(res.maxrecord);
 23         maxRecord.x = 10;
 24         maxRecord.y = this.size.height - maxRecord.height - 20;
 25         this.addChild(maxRecord, 1);
 26 
 27         var maxScore = new myImage(res.maxscore);
 28         maxScore.x = maxRecord.x + maxRecord.width + 30;
 29         maxScore.y = maxRecord.y;
 30         this.addChild(maxScore, 1);
 31 
 32         this.maxScoreLabel = new myText(this.maxScore.toString(), white, 26);
 33         this.maxScoreLabel.x = maxScore.x+(maxScore.width - this.maxScoreLabel.width)/2;
 34         this.maxScoreLabel.y = maxScore.y;
 35         this.addChild(this.maxScoreLabel, 2);
 36         //暂停和继续游戏控制按钮
 37         var pauseGameBtn = new myButton(res.pause);
 38         pauseGameBtn.x = this.size.width - pauseGameBtn.width - 10;
 39         pauseGameBtn.y = this.maxScoreLabel.y;
 40         this.addChild(pauseGameBtn, 1);
 41         pauseGameBtn.addTouchEventListener(this.pauseGameBtnFunc, this);
 42         //过关text
 43         var guoguanImg = new myImage(res.guoguan);
 44         guoguanImg.x = 0;
 45         guoguanImg.y = maxRecord.y - guoguanImg.height - 20;
 46         this.addChild(guoguanImg, 1);
 47         //当前关卡
 48         var currentLevelImg = new myImage(res.level);
 49         currentLevelImg.x = guoguanImg.x + guoguanImg.width;
 50         currentLevelImg.y = guoguanImg.y;
 51         this.addChild(currentLevelImg, 1);
 52         
 53         this.currentLevel = new myText(this.levelNumber.toString(), white, 24);
 54         this.currentLevel.x = currentLevelImg.x + (currentLevelImg.width -this.currentLevel.width)/2
 55         this.currentLevel.y = currentLevelImg.y;
 56         this.addChild(this.currentLevel, 1);
 57         //目标分数
 58         var targetImg = new myImage(res.target);
 59         targetImg.x = currentLevelImg.x + currentLevelImg.width + 20;
 60         targetImg.y = currentLevelImg.y;
 61         this.addChild(targetImg, 1);
 62         
 63         var targetImgbg = new myImage(res.targetBar);
 64         targetImgbg.x = this.size.width - targetImgbg.width - 10;
 65         targetImgbg.y = targetImg.y;
 66         this.addChild(targetImgbg, 1);
 67         
 68         var targetScore = new myText(this.standardScore.toString(), white, 25);
 69         targetScore.x = targetImgbg.x +(targetImgbg.width - targetScore.width)/2;
 70         targetScore.y = targetImgbg.y;
 71         this.addChild(targetScore, 1);
 72         //得分
 73         var getScore = new myImage(res.defen);
 74         getScore.x = this.size.width - getScore.width >> 1;
 75         getScore.y = targetScore.y - getScore.height - 10;
 76         this.addChild(getScore, 1);
 77         
 78         var getScoreBg = new myImage(res.defenBar);
 79         getScoreBg.x = this.size.width - getScoreBg.width >> 1;
 80         getScoreBg.y = getScore.y - getScoreBg.height - 10;
 81         this.addChild(getScoreBg, 1);
 82         
 83         this.getScoreNum = new myText(this.maxScore.toFixed(0), white, 25);
 84         this.getScoreNum.setAnchorPoint(0.5, 0);
 85         this.getScoreNum.x = this.size.width/2;
 86         this.getScoreNum.y = getScoreBg.y;
 87         this.addChild(this.getScoreNum, 1);
 88     },
 89     //暂停和继续游戏控制按钮侦听函数
 90     pauseGameBtnFunc:function(target, state)
 91     {
 92         if(state == ccui.Widget.TOUCH_ENDED)//松开
 93         {
 94             if(this.isPause)
 95             {
 96                 target.setOpacity(255);
 97                 this.isPause = false;
 98             }
 99             else
100             {
101                 target.setOpacity(150);
102                 this.isPause = true;
103             }
104         }
105     },
106     //更新游戏得分
107     updateGameScore:function(starList)
108     {
109         cc.log(this.scoreNumber+"  &&&&&&&&&&&&***************************")
110         var num = starList.length;
111         this.tempScore = 5*num*num;    
112         this.intermediaryScore = this.tempScore + this.scoreNumber;
113         //判断是否过关
114         this.jugementOverLevel();
115         //一次性获得一定分数的特效显示
116         this.effectOn();
117         this.schedule(this.showAddScoreProgress);
118         //获得分数特效1.2秒
119         var score = ccui.TextField.create();
120         score.setText("+"+this.tempScore.toString());
121         score.setPosition(cc.p(starList[0].x, starList[0].y));
122         score.setFontSize(30);
123         score.setColor(cc.color(255, 0, 0));//随机颜色
124         score.scale = 0.5;
125         this.parent.addChild(score, 10);
126         var scaleTo = cc.scaleTo(1.2, 2.2, 2.2);//放大
127         var moveTo = cc.moveTo(1.2, cc.p(score.x, score.y + 30));//移动
128         var callFunc = cc.CallFunc.create(function(){score.removeFromParent()}, this);//动作播放完成的回调函数
129         var spawn = cc.Spawn.create(scaleTo, moveTo);//同时播放动作
130         var sequence = cc.Sequence.create(spawn, callFunc);//按顺序播放动作
131         score.runAction(sequence);
132         //每次消灭星星,获得分数,都要检测游戏是否结束
133         GAMESTARLAYOUT.checkGameOver();
134     },
135     //判断是否过关
136     jugementOverLevel:function()
137     {
138         //过关
139         if(this.scoreNumber >= this.standardScore)
140         {
141             if(!this.isPassed)
142             {
143                 this.isPassed = true;
144                 this.passedLevelEffect(res.win);
145                 this.playerLevel++;
146                 this.saveInformation();
147             }
148             else
149             {
150                 return;
151             }
152         }
153     },
154     //过关特效显示
155     passedLevelEffect:function(str)
156     {
157         var win = new myImage(str);
158         win.setAnchorPoint(0.5, 0.5);
159         win.x = 480/2;
160         win.y = 820;
161         this.parent.addChild(win, 30);
162         var moveTo = cc.moveTo(1.2,cc.p(240, 400));
163         var scaleTo = cc.scaleTo(1.2, 1.3, 1.3);
164         var easeOut = moveTo.clone().easing(cc.easeInOut(5.0));
165         var sparn = cc.spawn(easeOut, scaleTo);
166         var fadeIn = cc.FadeOut.create(1);
167         var callFunc = cc.callFunc(function(){win.removeFromParent()}, this);
168         var sequenct = cc.sequence(sparn, fadeIn , callFunc);
169         win.runAction(sequenct);
170     },
171     //一次性获得一定分数的特效显示
172     effectOn:function()
173     {
174         var imgArr = [res.bang, res.geili, res.ku, res.niu];
175         //随机取一张鼓励的图
176         var random = Math.floor(Math.random()*imgArr.length);
177         var currentImg = imgArr[random];
178         if(this.tempScore >= 180)//6个星星
179         {
180             var effect = new myImage(currentImg);
181             effect.setAnchorPoint(0.5, 0.5);
182             effect.x = 480/2;
183             effect.y = 800/2;
184             effect.setOpacity(0);
185             this.parent.addChild(effect, 20);
186             
187             var fadeIn = cc.FadeIn.create(1);
188             var scaleTo = cc.scaleTo(1, 1.2, 1.2);
189             var spawn = cc.spawn(fadeIn, scaleTo);
190             var fadeOut = cc.FadeOut.create(1.2);
191             var callFunc = cc.CallFunc.create(function(){effect.removeFromParent()}, this);
192             var sequence = cc.sequence(spawn, fadeOut, callFunc);
193             effect.runAction(sequence);
194         }
195     },
196     //展现分数增长过程
197     showAddScoreProgress:function()
198     {
199         if(this.scoreNumber < this.intermediaryScore)
200         {
201             this.scoreNumber += 8;
202             this.getScoreNum.setText(this.scoreNumber.toFixed(0));
203         }
204         else
205         {
206             this.cancelSchedule();
207         }
208     },
209     //取消定时器
210     cancelSchedule:function()
211     {
212         this.unschedule(this.showAddScoreProgress);
213         
214         this.scoreNumber += this.intermediaryScore;
215         this.getScoreNum.setText(this.scoreNumber.toFixed(0));
216     },
217     //初始化
218     zinit:function()
219     {
220         GAMETOP = this;
221         this.playerGameData = playerGameData;//给玩家信息定义一个新的实例
222         this.levelNumber = this.playerGameData.currentLevel;
223         this.intermediaryScore = 0;//当前分数与本次获得的分数之和
224         this.tempScore = 0;//本次消除获得的分数
225         this.maxScore = this.playerGameData.maxScore;//游戏最高得分
226         this.scoreNumber = this.playerGameData.gameScore;//游戏界面显示的分数
227         this.playerLevel = this.playerGameData.currentLevel;//关卡
228         this.size = cc.size(480, 300);
229         this.setSize(this.size);
230         //获得当前关卡的目标分数
231         for(var i = 0; i < levelData.length; i++)
232         {
233             if(this.levelNumber == levelData[i].level)
234             {
235                 this.standardScore = levelData[i].standards;
236                 break;
237             }
238         }
239     },
240     //离开场景调用的方法
241     onExit:function()
242     {
243         this._super();
244         this.saveInformation();
245     },
246     saveInformation:function()
247     {
248         this.playerGameData = playerGameData;//给玩家信息定义一个新的实例
249         this.playerGameData.gameScore = this.scoreNumber;//游戏得分
250         this.playerGameData.maxScore = this.scoreNumber;//最高得分
251         this.playerGameData.currentLevel = this.playerLevel;//关卡
252         PlayerLocalData.setItem(this.playerGameData);
253     }
254 });
 
   

 

 
 
  

 


/**************************effect image*******************************/
 
 

 

转载于:https://www.cnblogs.com/zfsSuperDream/p/4057675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值