Cocos2d JS 之消灭星星(—) 游戏初始化场景的建立_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Cocos2d JS 之消灭星星(—) 游戏初始化场景的建立

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

 2014/10/27 23:05:05  Developer_方方  程序员俱乐部  我要评论(0)
  • 摘要:消灭星星是一款经典的益智手游,出于业余爱好,在cocoscodeide开发平台写一次;有关本游戏的玩法就不说了,我们直接上代码1/*2*游戏初始化场景的建立3*/4varGameInitializeScene=ccui.Layout.extend(5{6size:null,7isMusic:true,8ctor:function()9{10this._super();11this.zinit();12this.setTopInfor();13this.setBlinkAction()
  • 标签:游戏 建立 JS

    消灭星星是一款经典的益智手游,出于业余爱好,在cocos code ide开发平台写一次;有关本游戏的玩法就不说了,我们直接上代码

  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                     cc.log("newGame");
 81                         break;
 82                 case "continueGame":
 83                     cc.log("continueGame");
 84                     break;
 85                 case "helpGame":
 86                     cc.log("helpGame");
 87                     break;
 88                 case "exitGame":
 89                     cc.log("exitGame");
 90                     break;
 91             }
 92         }
 93     },
 94     //设置三个Blink图片分别从屏幕左右出现动画
 95     setBlinkAction:function()
 96     {
 97         var blink1 = new myImage(res.blink1);
 98         blink1.x = -blink1.width - 20;
 99         blink1.y = this.size.height - blink1.height - 65;
100         this.addChild(blink1, 1);
101         var moveTo1 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2, blink1.y));
102         blink1.runAction(moveTo1);
103         
104         var blink2 = new myImage(res.blink2);
105         blink2.x = this.size.width + 20;
106         blink2.y = blink1.y - blink2.height+40;
107         this.addChild(blink2, 1);
108         var moveTo2 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 - 30, blink2.y));
109         blink2.runAction(moveTo2);
110         
111         var blink3 = new myImage(res.blink3);
112         blink3.x = blink1.x;
113         blink3.y = blink2.y - blink3.height+70;
114         this.addChild(blink3, 1);
115         var moveTo3 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 + 50, blink3.y));
116         blink3.runAction(moveTo3);
117     },
118     //设置游戏初始化界面顶部显示信息(最高纪录、声音控制)
119     setTopInfor:function()
120     {
121         var maxRecord = new myImage(res.maxrecord);
122         maxRecord.x = 10;
123         maxRecord.y = this.size.height - maxRecord.height - 20;
124         this.addChild(maxRecord, 1);
125         
126         var maxScore = new myImage(res.maxscore);
127         maxScore.x = maxRecord.x + maxRecord.width + 30;
128         maxScore.y = maxRecord.y;
129         this.addChild(maxScore, 1);
130         
131         var maxScoreLabel = new myText("0", white, 26);
132         maxScoreLabel.x = maxScore.x+(maxScore.width - maxScoreLabel.width)/2;
133         maxScoreLabel.y = maxScore.y;
134         this.addChild(maxScoreLabel, 2);
135         
136         var laba = new myButton(res.labaok);
137         laba.x = this.size.width - laba.width - 5;
138         laba.y = maxScore.y;
139         this.addChild(laba, 1);
140         laba.addTouchEventListener(this.controlLabaFunc, this);
141     },
142     //喇叭控制响应侦听函数
143     controlLabaFunc:function(target, state)
144     {
145         if(state == ccui.Widget.TOUCH_ENDED)//松开
146         {
147             if(this.isMusic)//设为静音
148             {
149                 target.loadTextures(res.labano, "");
150                 this.isMusic = false;
151             }
152             else    //播放音乐
153             {
154                 target.loadTextures(res.labaok, "");
155                 this.isMusic = true;
156             }
157         }
158     },
159     //初始化函数
160     zinit:function()
161     {
162         //设置布局大小
163         this.size = cc.size(480, 800);
164         this.setSize(this.size);
165         //实例化背景图片
166         var backGround = new myImage(res.mainbacktop);
167         backGround.y = this.size.height - backGround.height;
168         this.addChild(backGround, 0);
169         var backGround1 = new myImage(res.mainbackbottom);
170         this.addChild(backGround1, 0);
171     }
172 });
173 
174 
175 GameInitializeScene.createScene = function()
176 {
177     var scene = cc.Scene.create();
178     var layout = new GameInitializeScene();
179     scene.addChild(layout);
180     return scene;
181 };
/**************effect Image**********************/


 

上一篇: 在代码中设置TextView 的字体颜色 下一篇: 没有下一篇了!
发表评论
用户名: 匿名