1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
var GameLayer = cc.Layer.extend({ touchStartX:0, touchStartY:0, bullets:[], enemies:[], tools:[], ctor: function () { this._super(); this.touchStartX = 0; this.touchStartY = 0; this.bullets = []; this.enemies = []; this.tools = []; cc.audioEngine.playMusic("res/sound/game_music.mp3",true); cc.spriteFrameCache.addSpriteFrames(res.shoot_background_plist); cc.spriteFrameCache.addSpriteFrames(res.shoot_plist); var bg = new Background(false); bg.setPosition(0,0); this.addChild(bg); var player = new Player(this); player.setPosition(cc.winSize.width / 2, -player.height / 2); this.addChild(player); player.setTag(1); this.schedule(function(){ this.createEnemy(1); },Global.createEnemySpeed(1)); this.schedule(function(){ this.createEnemy(2); },Global.createEnemySpeed(2)); this.schedule(function(){ this.createEnemy(3); },Global.createEnemySpeed(3)); this.schedule(function(){ this.createTool(1); },Global.createToolSpeed(1)); this.schedule(function(){ this.createTool(2); },Global.createToolSpeed(2)); var bombNor = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("bomb.png")); var bombSelected = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("bomb.png")); bombSelected.setPosition(-bombSelected.width/4,-bombSelected.height/4); bombSelected.setScale(1.5); var bombBtn = new cc.MenuItemSprite( bombNor, bombSelected, function () { var bombNum = this.getChildByTag(3); if(parseInt(bombNum.getString().slice(1))==0){ return; } var blowEnemy = []; for(var i in this.enemies){ var enemy = this.enemies[i]; blowEnemy.push(enemy); } for(var j in blowEnemy){ blowEnemy[j].blowUp(); } bombNum.setString("X"+(parseInt(bombNum.getString().slice(1))-1)); }, this); bombBtn.setPosition(50+bombBtn.width/2,50+bombBtn.height/2); var bombMenu = new cc.Menu(bombBtn); bombMenu.setPosition(0,0); bombMenu.setAnchorPoint(0,0); this.addChild(bombMenu); var bombNum = new cc.LabelBMFont("X2",res.font); bombNum.setAnchorPoint(0,0.5); bombNum.setPosition(bombBtn.getPositionX()+bombBtn.width/2+50,bombBtn.getPositionY()); bombNum.setTag(3); this.addChild(bombNum); var pauseBtn = new cc.MenuItemSprite( new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("game_pause_nor.png")), new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("game_pause_pressed.png")), function () { cc.audioEngine.pauseAllEffects(); cc.audioEngine.pauseMusic(); pauseBtn.setEnabled(false); cc.director.pause(); this.addChild(new PauseLayer(pauseBtn),10); }, this); var pauseMenu = new cc.Menu(pauseBtn); pauseMenu.setPosition(20+pauseBtn.width/2,cc.winSize.height-pauseBtn.height/2-20); pauseMenu.setAnchorPoint(0,0); this.addChild(pauseMenu); var score = new cc.LabelBMFont("0",res.font); score.setAnchorPoint(0,0.5); score.setPosition(pauseMenu.getPositionX()+pauseBtn.width/2+50,pauseMenu.getPositionY()); score.setTag(2); this.addChild(score); this.schedule(this.collision); return true; }, collision:function(){ var bullets = this.bullets; var enemies = this.enemies; var tools = this.tools; var score = parseInt(this.getChildByTag(2).getString()); for(var i in enemies){ var enemy = enemies[i]; var player = this.getChildByTag(1); if(cc.rectIntersectsRect(enemy.getBoundingBox(),player.getBoundingBox())){ this.unschedule(this.collision); player.blowUp(); cc.audioEngine.stopMusic("res/sound/game_music.mp3"); cc.audioEngine.playEffect("res/sound/game_over.mp3"); this.scheduleOnce(function() { cc.director.runScene(new cc.TransitionFade(1,new OverScene(score))); },2); } for(var m in tools){ var tool = tools[m]; if(cc.rectIntersectsRect(tool.getBoundingBox(),player.getBoundingBox())){ switch(tool.type){ case 1: cc.audioEngine.playEffect("res/sound/get_double_laser.mp3"); player.shootDoubleBegin(); break; case 2: cc.audioEngine.playEffect("res/sound/get_bomb.mp3"); var bomb = this.getChildByTag(3); bomb.setString("X"+(parseInt(bomb.getString().slice(1))+1)); bomb.runAction(cc.sequence(cc.scaleTo(0.1,1.2),cc.scaleTo(0.1,1))); break; } tool.remove(); } } for(var j in bullets){ var bullet = bullets[j]; if(cc.rectIntersectsRect(enemy.getBoundingBox(),bullet.getBoundingBox())){ enemy.hit(); bullet.remove(); } } } }, addScore:function(type){ var score = this.getChildByTag(2); var addScore = 0; var curScore = parseInt(score.getString()); switch(type){ case 1: addScore = 100 + Math.ceil(Math.random()*(curScore/1000)); break; case 2: addScore = 200 + Math.ceil(Math.random()*(curScore/1000)); break; case 3: addScore = 500 + Math.ceil(Math.random()*(curScore/1000)); break; } score.setString(curScore+addScore); }, createEnemy:function(type){ var enemy = new Enemy(type,this); var randomX = Math.random()*(cc.winSize.width-enemy.width/2-enemy.width/2)+enemy.width/2; enemy.setPosition(randomX,cc.winSize.height+enemy.height/2); this.addChild(enemy); this.enemies.push(enemy); }, createTool:function(type){ var tool = new Tool(type,this); var randomX = Math.random()*(cc.winSize.width-tool.width/2-tool.width/2)+tool.width/2; tool.setPosition(randomX,cc.winSize.height+tool.height/2); this.addChild(tool); this.tools.push(tool); }, onEnter:function(){ this._super(); cc.eventManager.addListener({ event:cc.EventListener.TOUCH_ONE_BY_ONE, swallowTouches:true, onTouchBegan:this.touchbegan, onTouchMoved:this.touchmoved, onTouchEnded:this.touchended },this); return true; }, touchbegan:function(touch,event){ event.getCurrentTarget().touchStartX = touch.getLocation().x; event.getCurrentTarget().touchStartY = touch.getLocation().y; return true; }, touchmoved:function(touch,event){ var touchX = touch.getLocation().x; var touchY = touch.getLocation().y; var touchStartX = event.getCurrentTarget().touchStartX; var touchStartY = event.getCurrentTarget().touchStartY; var player = event.getCurrentTarget().getChildByTag(1); if(player!=null){ player.moveBy(touchX-touchStartX,touchY-touchStartY); event.getCurrentTarget().touchStartX = touchX; event.getCurrentTarget().touchStartY = touchY; } return true; }, touchended:function(touch,event){ return true; } });
var GameScene = cc.Scene.extend({ onEnter: function () { this._super(); var layer = new GameLayer(); this.addChild(layer); } });
|