Cocos2d JS 之消灭星星(—)游戏初始化场景的建立,cocos2djs
消灭星星是一款经典的益智手游,出于业余爱好,在cocos code ide开发平台写一次;有关本游戏的玩法就不说了,我们直接上代码
1 /*
2* 游戏初始化场景的建立
3*/
4 var GameInitializeScene = ccui.Layout.extend(
5 {
6size:null,
7isMusic:true,
8ctor:function()
9{
10this._super();
11this.zinit();
12this.setTopInfor();
13this.setBlinkAction();
14this.scheduleOnce(this.setGameButton, 1);//延时1s
15},
16//设置与玩家交互的按钮(新游戏、继续游戏、帮助、退出)
17setGameButton:function()
18{
19var gap = 7;
20var a = 340, b = 275, c = 210, d = 145;
21//新游戏
22var newGameBtn = new myButton(res.newgame);
23var endX = this.size.width - newGameBtn.width >> 1;
24var endY = this.size.height+100;;
25newGameBtn.name = "newGame";
26newGameBtn.x = endX;
27newGameBtn.y = endY;
28this.addChild(newGameBtn, 1);
29//action1
30var moveTo1 = cc.MoveTo.create(5, cc.p(endX, a));
31var easeOut1 = moveTo1.clone().easing(cc.easeElasticOut());
32newGameBtn.runAction(easeOut1);
33
34//继续游戏
35var continueGameBtn = new myButton(res.resume);
36continueGameBtn.name = "continueGame";
37continueGameBtn.x = endX;
38continueGameBtn.y = endY
39this.addChild(continueGameBtn, 1);
40//action2
41var moveTo2 = cc.MoveTo.create(4, cc.p(endX, b));
42var easeOut2 = moveTo2.clone().easing(cc.easeElasticOut());
43continueGameBtn.runAction(easeOut2);
44
45//帮助
46var helpGameBtn = new myButton(res.help);
47helpGameBtn.name = "helpGame";
48helpGameBtn.x = endX;
49helpGameBtn.y = endY;
50this.addChild(helpGameBtn, 1);
51//action3
52var moveTo3 = cc.MoveTo.create(3, cc.p(endX, c));
53var easeOut3 = moveTo3.clone().easing(cc.easeElasticOut());
54helpGameBtn.runAction(easeOut3);
55
56//退出
57var exitGameBtn = new myButton(res.exit);
58exitGameBtn.name = "exitGame";
59exitGameBtn.x = endX;
60exitGameBtn.y = endY;
61this.addChild(exitGameBtn, 1);
62//action4
63var moveTo4 = cc.MoveTo.create(2, cc.p(endX, d));
64var easeOut4 = moveTo4.clone().easing(cc.easeElasticOut());
65exitGameBtn.runAction(easeOut4);
66
67newGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
68continueGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
69helpGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
70exitGameBtn.addTouchEventListener(this.btnControlGameFunc, this);
71},
72//按钮侦听函数
73btnControlGameFunc:function(target, state)
74{
75if(state == ccui.Widget.TOUCH_ENDED)//松开
76{
77switch (target.name)
78{
79case "newGame":
80cc.log("newGame");
81break;
82case "continueGame":
83cc.log("continueGame");
84break;
85case "helpGame":
86cc.log("helpGame");
87break;
88case "exitGame":
89cc.log("exitGame");
90break;
91}
92}
93},
94//设置三个Blink图片分别从屏幕左右出现动画
95setBlinkAction:function()
96{
97var blink1 = new myImage(res.blink1);
98blink1.x = -blink1.width - 20;
99blink1.y = this.size.height - blink1.height - 65;
100this.addChild(blink1, 1);
101var moveTo1 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2, blink1.y));
102blink1.runAction(moveTo1);
103
104var blink2 = new myImage(res.blink2);
105blink2.x = this.size.width + 20;
106blink2.y = blink1.y - blink2.height+40;
107this.addChild(blink2, 1);
108var moveTo2 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 - 30, blink2.y));
109blink2.runAction(moveTo2);
110
111var blink3 = new myImage(res.blink3);
112blink3.x = blink1.x;
113blink3.y = blink2.y - blink3.height+70;
114this.addChild(blink3, 1);
115var moveTo3 = cc.moveTo(1, cc.p((this.size.width-blink1.width)/2 + 50, blink3.y));
116blink3.runAction(moveTo3);
117},
118//设置游戏初始化界面顶部显示信息(最高纪录、声音控制)
119setTopInfor:function()
120{
121var maxRecord = new myImage(res.maxrecord);
122maxRecord.x = 10;
123maxRecord.y = this.size.height - maxRecord.height - 20;
124this.addChild(maxRecord, 1);
125
126var maxScore = new myImage(res.maxscore);
127maxScore.x = maxRecord.x + maxRecord.width + 30;
128maxScore.y = maxRecord.y;
129this.addChild(maxScore, 1);
130
131var maxScoreLabel = new myText("0", white, 26);
132maxScoreLabel.x = maxScore.x+(maxScore.width - maxScoreLabel.width)/2;
133maxScoreLabel.y = maxScore.y;
134this.addChild(maxScoreLabel, 2);
135
136var laba = new myButton(res.labaok);
137laba.x = this.size.width - laba.width - 5;
138laba.y = maxScore.y;
139this.addChild(laba, 1);
140laba.addTouchEventListener(this.controlLabaFunc, this);
141},
142//喇叭控制响应侦听函数
143controlLabaFunc:function(target, state)
144{
145if(state == ccui.Widget.TOUCH_ENDED)//松开
146{
147if(this.isMusic)//设为静音
148{
149target.loadTextures(res.labano, "");
150this.isMusic = false;
151}
152else//播放音乐
153{
154target.loadTextures(res.labaok, "");
155this.isMusic = true;
156}
157}
158},
159//初始化函数
160zinit:function()
161{
162//设置布局大小
163this.size = cc.size(480, 800);
164this.setSize(this.size);
165//实例化背景图片
166var backGround = new myImage(res.mainbacktop);
167backGround.y = this.size.height - backGround.height;
168this.addChild(backGround, 0);
169var backGround1 = new myImage(res.mainbackbottom);
170this.addChild(backGround1, 0);
171}
172 });
173
174
175 GameInitializeScene.createScene = function()
176 {
177var scene = cc.Scene.create();
178var layout = new GameInitializeScene();
179scene.addChild(layout);
180return scene;
181 };/**************effect Image**********************/
http://www.bkjia.com/Androidjc/900966.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/900966.htmlTechArticleCocos2d JS 之消灭星星(—) 游戏初始化场景的建立,cocos2djs 消灭星星是一款经典的益智手游,出于业余爱好,在cocos code ide开发平台写一次;有关...