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*******************************/
相關文章
相關標籤/搜索