Cocos2d JS 之消滅星星(—) 遊戲初始化情境的建立,cocos2djs
消滅星星是一款經典的益智手遊,出於業餘愛好,在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**********************/