Cocos2d JS 之消灭星星(—) 游戏初始化场景的建立


  1 /*
  2  * 游戏初始化场景的建立
  3  */
  4 var GameInitializeScene = ccui.Layout.extend(
  5 {
  6     size:null,
  7     isMusic:true,
  8     ctor:function()
  9     {
 10         this._super();
 11         this.zinit();
 12         this.setTopInfor();
 13         this.setBlinkAction();
 14         this.scheduleOnce(this.setGameButton, 1);//延时1s
 15     },
 16     //设置与玩家交互的按钮(新游戏、继续游戏、帮助、退出)
 17     setGameButton:function()
 18     {
 19         var gap = 7;
 20         var a = 340, b = 275, c = 210, d = 145;
 21         //新游戏
 22         var newGameBtn = new myButton(res.newgame);
 23         var endX = this.size.width - newGameBtn.width >> 1;
 24         var endY = this.size.height+100;;
 25         newGameBtn.name = "newGame";
 26         newGameBtn.x = endX;
 27         newGameBtn.y = endY;
 28         this.addChild(newGameBtn, 1);
 29         //action1
 30         var moveTo1 = cc.MoveTo.create(5, cc.p(endX, a));
 31         var easeOut1 = moveTo1.clone().easing(cc.easeElasticOut());
 32         newGameBtn.runAction(easeOut1);
 33         
 34         //继续游戏
 35         var continueGameBtn = new myButton(res.resume);
 36         continueGameBtn.name = "continueGame";
 37         continueGameBtn.x = endX;
 38         continueGameBtn.y = endY
 39         this.addChild(continueGameBtn, 1);
 40         //action2
 41         var moveTo2 = cc.MoveTo.create(4, cc.p(endX, b));
 42         var easeOut2 = moveTo2.clone().easing(cc.easeElasticOut());
 43         continueGameBtn.runAction(easeOut2);
 44         
 45         //帮助
 46         var helpGameBtn = new myButton(res.help);
 47         helpGameBtn.name = "helpGame";
 48         helpGameBtn.x = endX;
 49         helpGameBtn.y = endY;
 50         this.addChild(helpGameBtn, 1);
 51         //action3
 52         var moveTo3 = cc.MoveTo.create(3, cc.p(endX, c));
 53         var easeOut3 = moveTo3.clone().easing(cc.easeElasticOut());
 54         helpGameBtn.runAction(easeOut3);
 55         
 56         //退出
 57         var exitGameBtn = new myButton(res.exit);
 58         exitGameBtn.name = "exitGame";
 59         exitGameBtn.x = endX;
 60         exitGameBtn.y = endY;
 61         this.addChild(exitGameBtn, 1);
 62         //action4
 63         var moveTo4 = cc.MoveTo.create(2, cc.p(endX, d));
 64         var easeOut4 = moveTo4.clone().easing(cc.easeElasticOut());
 65         exitGameBtn.runAction(easeOut4);
 66         
 67         newGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
 68         continueGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
 69         helpGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
 70         exitGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
 71     },
 72     //按钮侦听函数
 73     btnControlGameFunc:function(target, state)
 74     {
 75         if(state == ccui.Widget.TOUCH_ENDED)//松开
 76         {
 77             switch (target.name)
 78             {
 79                 case "newGame":            //进入新游戏
 80                     var newGameScene = TransitionScene.createScene(true);
 81                     cc.director.runScene(cc.TransitionFade.create(1, newGameScene));
 82                     cc.log("newGame");
 83                     break;
 84                 case "continueGame"://继续游戏
 85                     var newGameScene = TransitionScene.createScene(false);
 86                     cc.director.runScene(cc.TransitionFade.create(1, newGameScene));
 87                     break;
 88                 case "helpGame"://游戏帮助
 89                     var helpScene = GameHelpLayout.createScene();
 90                     cc.director.runScene(cc.TransitionFade.create(1, helpScene));
 91                     break;
 92                 case "exitGame"://退出游戏
 93                     cc.log("exitGame");
 94                     break;
 95             }
 96         }
 97     },
 98     //设置三个Blink图片分别从屏幕左右出现动画
 99     setBlinkAction:function()
100     {
101         var blink1 = new myImage(res.blink1);
102         blink1.x = -blink1.width - 20;
103         blink1.y = this.size.height - blink1.height - 65;
104         this.addChild(blink1, 1);
105         var moveTo1 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2, blink1.y));
106         blink1.runAction(moveTo1);
107         
108         var blink2 = new myImage(res.blink2);
109         blink2.x = this.size.width + 20;
110         blink2.y = blink1.y - blink2.height+40;
111         this.addChild(blink2, 1);
112         var moveTo2 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 - 30, blink2.y));
113         blink2.runAction(moveTo2);
114         
115         var blink3 = new myImage(res.blink3);
116         blink3.x = blink1.x;
117         blink3.y = blink2.y - blink3.height+70;
118         this.addChild(blink3, 1);
119         var moveTo3 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 + 50, blink3.y));
120         blink3.runAction(moveTo3);
121     },
122     //设置游戏初始化界面顶部显示信息(最高纪录、声音控制)
123     setTopInfor:function()
124     {
125         var maxRecord = new myImage(res.maxrecord);
126         maxRecord.x = 10;
127         maxRecord.y = this.size.height - maxRecord.height - 20;
128         this.addChild(maxRecord, 1);
129         
130         var maxScore = new myImage(res.maxscore);
131         maxScore.x = maxRecord.x + maxRecord.width + 30;
132         maxScore.y = maxRecord.y;
133         this.addChild(maxScore, 1);
134         //最高纪录
135         var maxScoreLabel = new myText(this.maxScore.toString(), white, 26);
136         maxScoreLabel.x = maxScore.x+(maxScore.width - maxScoreLabel.width)/2;
137         maxScoreLabel.y = maxScore.y;
138         this.addChild(maxScoreLabel, 2);
139         //声音喇叭
140         var laba = new myButton(res.labaok);
141         laba.x = this.size.width - laba.width - 5;
142         laba.y = maxScore.y;
143         this.addChild(laba, 1);
144         laba.addTouchEventListener(this.controlLabaFunc, this);
145     },
146     //喇叭控制响应侦听函数
147     controlLabaFunc:function(target, state)
148     {
149         if(state == ccui.Widget.TOUCH_ENDED)//松开
150         {
151             if(this.isMusic)//设为静音
152             {
153                 target.loadTextures(res.labano, "");
154                 this.isMusic = false;
155             }
156             else    //播放音乐
157             {
158                 target.loadTextures(res.labaok, "");
159                 this.isMusic = true;
160             }
161         }
162     },
163     //初始化函数
164     zinit:function()
165     {
166         //设置布局大小
167         this.size = cc.size(480, 800);
168         this.setSize(this.size);
169         //实例化背景图片
170         var backGround = new myImage(res.mainbacktop);
171         backGround.y = this.size.height - backGround.height;
172         this.addChild(backGround, 0);
173         var backGround1 = new myImage(res.mainbackbottom);
174         this.addChild(backGround1, 0);
175         //玩家本地数据
176         this.playerGameData = PlayerLocalData.getItem();
177         //这里要注意,第一次进入游戏时,this.playerGameData是一个数组,之后就变成对象了,这里确保游戏中统一用对象
178         if(this.playerGameData.length == true)
179         {
180             this.playerGameData = this.playerGameData[0];
181         }
182         else
183         {
184             this.playerGameData = this.playerGameData;
185         }
186         this.maxScore = this.playerGameData.maxScore;//游戏最高得分
187     }
188 });
189 
190 
191 GameInitializeScene.createScene = function()
192 {
193     var scene = cc.Scene.create();
194     var layout = new GameInitializeScene();
195     scene.addChild(layout);
196     return scene;
197 };


 

/**************effect Image**********************/

原文地址:https://www.cnblogs.com/zfsSuperDream/p/4054980.html