最近闲着没事,又想着做游戏了,自己没有什么好的想法所以就只能仿别的游戏了。微信打飞机是比较成功的一个游戏,而且还简单非常适合初学者去仿写(ps:网上的素材一大把)。说下程序里面的主要文件,SysMenu.js 是负责游戏开始画面,GameLayer.js是复制游戏主场景的,MyPlaneLayer.js是英雄飞机层,EnemyLayer.js 是敌机层,Enemy.js是敌机类,BulletLayer.js是子弹层,BombLayer.js是复制全屏炸弹和 双排子弹的层,ControlLayer.js是复制暂停 显示分数什么的一个层,PopupLayer.js则是弹出窗口。代码注释自认为写的还是比较详细的,大家一定都能看的懂,毕竟我也是初学者。
/** * Created by Administrator on 2014/9/25. */ var GameLayer = cc.Layer.extend({ winSize:null,//屏幕尺寸 origin:null,//坐标原点 myPlane:null,//英雄飞机 myPlaneLayer:null,//英雄飞机层 bulletLayer:null,//子弹层 enemyLayer:null,//敌机层 bombLayer:null,//炸弹层 controlLayer:null,//操作层 popupLayer:null,init:function () { ////////////////////////////// // 1. super init first this._super(); //精灵帧缓存 cc.SpriteFrameCache.getInstance().addSpriteFrames(s_BgList); cc.SpriteFrameCache.getInstance().addSpriteFrames(s_ShootList); //获取游戏屏幕的尺寸 this.winSize = cc.Director.getInstance().getWinSize(); //获取游戏屏幕的坐标原点 this.origin = cc.Director.getInstance().getVisibleOrigin(); //初始化整个屏幕里面的子弹数组 this._bullets = []; //添加背景图片 (实现背景图片滚动) var bg = cc.Sprite.createWithSpriteFrameName("background.png"); bg.setScaleX(this.winSize.width/480); bg.setScaleY(this.winSize.height/850); bg.setAnchorPoint(0,0); bg.setPosition(cc.p(0,0)); this.addChild(bg,1); //添加副本背景图片 var bg1 = cc.Sprite.createWithSpriteFrameName("background.png"); bg1.setScaleX(this.winSize.width/480); bg1.setScaleY(this.winSize.height/850); bg1.setAnchorPoint(0,0); bg1.setPosition(cc.p(0,0)); this.addChild(bg1,2); //为图片消除锯齿 bg.getTexture().setAliasTexParameters(); bg1.getTexture().setAliasTexParameters(); //设置计时器,执行滚动背景图片动作 this.schedule(this.UpdateBackground,0.1); //预加载音效 cc.AudioEngine.getInstance().preloadMusic(music_Game); //播放背景音乐 //cc.AudioEngine.getInstance().playMusic(music_Game,true); //添加飞机层 this.myPlaneLayer = MyPlaneLayer.create(); //添加子弹层 this.bulletLayer = BulletLayer.create(); this.bulletLayer.myPlaneLayer = this.myPlaneLayer; this.addChild(this.bulletLayer); //添加敌机层 this.enemyLayer = EnemyLayer.create(); this.bulletLayer.enemyLayer = this.enemyLayer; this.addChild(this.enemyLayer); //添加炸弹层 this.bombLayer = BombLayer.create(); this.addChild(this.bombLayer); //添加操作层 this.controlLayer = ControlLayer.create(); this.addChild(this.controlLayer); this.bulletLayer.controlLayer = this.controlLayer; this.controlLayer.enemyLayer = this.enemyLayer; this.enemyLayer.controlLayer = this.controlLayer; this.popupLayer = PopupLayer.create(); this.myPlaneLayer.popupLayer = this.popupLayer; this.enemyLayer.popupLayer = this.popupLayer; this.addChild(this.myPlaneLayer); //更新游戏 this.schedule(this.updateGame); return true; },UpdateBackground:function(){ //设置背景图片移动速度 var bgSpeed = -16; var i; var bg1,bg2; for( i in this.getChildren()){ //获取所有精灵,根据标签判断背景和 副本背景 if(this.getChildren()[i].getTag() == 1){ bg1 = this.getChildren()[i]; } if(this.getChildren()[i].getTag() == 2) { bg2 = this.getChildren()[i]; } } //背景图片每0.1秒向下移动2个单位 var bg1Next=bg1.getPositionY()-2; bg1.setPositionY(bg1Next); //副本背景图片为当前背景图片y加上屏幕高度 var bg2Next=bg1.getPositionY()+this.winSize.height; bg2.setPositionY(bg2Next); //如果副本背景图片移出屏幕,重新设置背景图片位置,达到循环滚动的效果 if (bg2.getPositionY()<=0) { bg1.setPositionY(0); } },updateGame:function(){ var enemysDelete = []; var i; //遍历屏幕上的每个敌机 for (i in this.enemyLayer._enemys) { var enemy = this.enemyLayer._enemys[ i ]; // 获得敌机的碰撞矩形 var enemyRect = enemy.getBoundingBox(); var bulletsDelete = []; // 对于每个敌机,遍历每个屏幕上的子弹,判断是否碰撞 for (i in this.bulletLayer._bullets) { var bullet = this.bulletLayer._bullets[ i ]; var bulletRect = bullet.getBoundingBox(); // 判断两个矩形是否碰撞 if (cc.rectIntersectsRect(bulletRect,enemyRect)) { // 碰撞则将子弹加入待删除列表 bulletsDelete.push(bullet); //敌机生命值减少 enemy.hurt(); } } // 如果待删除的子弹数组的内容大于零,说明敌机碰到了子弹,将敌机加入待删除数组 if (bulletsDelete.length > 0) { //如果飞机生命值小于等于0说明飞机被摧毁 if(enemy.HP <=0 ){ enemysDelete.push(enemy); //敌机被摧毁,英雄飞机得分 this.controlLayer.score += enemy.score; this.popupLayer.score += enemy.score; console.log("this.popupLayer.score"+this.popupLayer.score); //this.myPlaneLayer.score = this.controlLayer.score; } // //添加敌机摧毁音效 // cc.AudioEngine.getInstance().playEffect(s_Music); } //删除发生碰撞的每个子弹 for (i in bulletsDelete) { var bullet = bulletsDelete[ i ]; var index = this.bulletLayer._bullets.indexOf(bullet); if (index > -1) { this.bulletLayer._bullets.splice(index,1); } this.bulletLayer.removeChild(bullet); } bulletsDelete = null; } //删除发生碰撞的每个敌机 for (i in enemysDelete) { var enemy = enemysDelete[ i ]; var index = this.enemyLayer._enemys.indexOf(enemy); if (index > -1) { this.enemyLayer._enemys.splice(index,1); } enemy.destroy(); } enemysDelete = null; if(this.myPlaneLayer.isLife == 1){ //获取英雄飞机 this.myPlane = this.myPlaneLayer.getChildByTag(9999); //获取英雄飞机的碰撞矩形 var myPlaneRect = this.myPlane.getBoundingBox(); //遍历屏幕上的每一个飞机 for (i in this.enemyLayer._enemys) { var enemy = this.enemyLayer._enemys[ i ]; //获取敌机的碰撞矩形 var enemyRect = enemy.getBoundingBox(); // 判断两个矩形是否碰撞 if (cc.rectIntersectsRect(myPlaneRect,enemyRect)) { // 执行英雄飞机被摧毁动画 this.myPlaneLayer.destroy(); this.bulletLayer.stop(); this.controlLayer.destroy(); } } //遍历屏幕上的每一个炸弹礼包 for(i in this.bombLayer._bombs){ var bomb = this.bombLayer._bombs[i]; //获取炸弹礼包的碰撞矩形 var bombRect = bomb.getBoundingBox(); //判断两个矩形是否碰撞 if(cc.rectIntersectsRect(myPlaneRect,bombRect)){ this.bulletLayer.bombType = bomb.type; //执行飞机接受礼包 this.bulletLayer.getBomb(); this.bombLayer.removeChild(bomb); //从数组中移除 var index = this.bombLayer._bombs.indexOf(bomb); if(index >-1){ this.bombLayer._bombs.splice(index,1); } } } } } }); GameLayer.scene = function () { var scene = cc.Scene.create(); var layer = GameLayer.create(); scene.addChild(layer,1); return scene; }; GameLayer.create = function () { var sg = new GameLayer(); if (sg && sg.init()) { return sg; } return null; };
/** * Created by Administrator on 2014/9/25. */ var BulletLayer = cc.Layer.extend({ winSize:null,//英雄飞机 bulletDuration:2,//子弹穿越屏幕花费时间 myPlaneLayer:null,//英雄飞机层 type:1,//子弹类型 1:单排子弹 2:双排子弹 _bullets:null,//子弹数组 _enemyBullets:null,//敌机发出的子弹数组 _enemys:null,//敌机精灵数组 StrengthenBulletNum:0,//升级的导弹(两排导弹),能够坚持两分钟 bombType:null,//炸弹类型 1:双排子弹 2:全屏炸弹 enemyLayer:null,//敌机层 controlLayer:null,//操作层 init:function () { ////////////////////////////// // 1. super init first this._super(); //子弹数组声明 this._bullets = []; this._enemyBullets = []; this._enemys = []; //精灵帧缓存 cc.SpriteFrameCache.getInstance().addSpriteFrames(s_ShootList); //获取游戏屏幕的尺寸 this.winSize = cc.Director.getInstance().getWinSize(); //获取游戏屏幕的坐标原点 this.origin = cc.Director.getInstance().getVisibleOrigin(); if(this.type == 1){ this.unschedule(this.addDoubleBullet); this.schedule(this.addSingleBullet,0.2); }else{ this.unschedule(this.addSingleBullet); this.schedule(this.addDoubleBullet,0.2); } //更新子弹数量 this.schedule(this.updateBullet,0.2); //预加载音效 cc.AudioEngine.getInstance().preloadEffect(music_Bullet); return true; },addSingleBullet:function(){ //获取英雄飞机 this.myPlane = this.myPlaneLayer.getChildByTag(9999); //一排子弹 var bullet = cc.Sprite.createWithSpriteFrameName("bullet2.png"); //缩小尺寸 bullet.setScale(0.5); //为子弹精灵设置标签 bullet.setTag(777); // 根据飞机的位置,初始化子弹的位置 bullet.setPosition(cc.p(this.myPlane.getPosition().x,this.myPlane.getPosition().y+bullet.getContentSize().height)); // 一个移动的动作 // 第一个参数为移动到目标所需要花费的秒数,为了保持速度不变,需要按移动的距离与屏幕高度按比例计算出花费的秒数 var actionMove = cc.MoveTo.create(this.bulletDuration * ((this.winSize.height - this.myPlane.getPosition().y - bullet.getContentSize().height/2)/this.winSize.height),cc.p(this.myPlane.getPosition().x,this.origin.y + this.winSize.height*2 + bullet.getContentSize().height/2)); // 设置一个回调函数,移动完毕后回调spriteMoveFinished()方法。 var actionMoveDone = cc.CallFunc.create(this.spriteMoveFinished,this); // 让子弹执行动作 bullet.runAction(cc.Sequence.create(actionMove,actionMoveDone)); this.addChild(bullet); //将子弹精灵添加到数组中 this._bullets.push(bullet); //播放子弹音效 //cc.AudioEngine.getInstance().playEffect(music_Bullet); },addDoubleBullet:function(){ //获取英雄飞机 this.myPlane = this.myPlaneLayer.getChildByTag(9999); //两排子弹 var bullet = cc.Sprite.createWithSpriteFrameName("bullet2.png"); var bullet2 = cc.Sprite.createWithSpriteFrameName("bullet2.png"); //缩小尺寸 bullet.setScale(0.5); bullet2.setScale(0.5); //为子弹精灵设置标签 bullet.setTag(777); bullet2.setTag(777); // 根据飞机的位置,初始化子弹的位置 bullet.setPosition(cc.p(this.myPlane.getPosition().x - 10,this.myPlane.getPosition().y+bullet.getContentSize().height)); bullet2.setPosition(cc.p(this.myPlane.getPosition().x + 10,this.myPlane.getPosition().y+bullet2.getContentSize().height)); // 一个移动的动作 // 第一个参数为移动到目标所需要花费的秒数,为了保持速度不变,需要按移动的距离与屏幕高度按比例计算出花费的秒数 var actionMove = cc.MoveTo.create(this.bulletDuration * ((this.winSize.height - this.myPlane.getPosition().y - bullet.getContentSize().height/2)/this.winSize.height),cc.p(this.myPlane.getPosition().x - 10,this.origin.y + this.winSize.height*2 + bullet.getContentSize().height/2)); //设置一个回调函数,移动完毕后回调spriteMoveFinished()方法。 var actionMoveDone = cc.CallFunc.create(this.spriteMoveFinished,actionMoveDone)); var actionMove2 = cc.MoveTo.create(this.bulletDuration * ((this.winSize.height - this.myPlane.getPosition().y - bullet2.getContentSize().height/2)/this.winSize.height),cc.p(this.myPlane.getPosition().x +10,this.origin.y + this.winSize.height*2 + bullet2.getContentSize().height/2)); bullet2.runAction(cc.Sequence.create(actionMove2,actionMoveDone)); this.addChild(bullet); this.addChild(bullet2); //将子弹精灵添加到数组中 this._bullets.push(bullet); this._bullets.push(bullet2); //播放子弹音效 //cc.AudioEngine.getInstance().playEffect(music_Bullet); },stop:function(){ if(this.myPlaneLayer.isLife == 0){ if(this.type == 1){ this.unschedule(this.addSingleBullet); }else{ this.unschedule(this.addDoubleBullet); } this.unschedule(this.updateBullet); } },spriteMoveFinished:function(sprite){ //子弹精灵移出后,当前层删除该精灵 this.removeChild(sprite,true); if(sprite.getTag() == 777){ //从数组中移除 var index = this._bullets.indexOf(sprite); if(index >-1){ this._bullets.splice(index,1); } } if(sprite.getTag() == 444){ //从数组中移除 var index = this._enemyBullets.indexOf(sprite); if(index >-1){ this._enemyBullets.splice(index,1); } } },updateBullet:function(){ if(this.type == 2){ this.StrengthenBulletNum -=2; if(this.StrengthenBulletNum == 0){ this.unschedule(this.addDoubleBullet); this.schedule(this.addSingleBullet,0.2); } } },getBomb:function(){ //获取炸弹礼包 //双排子弹 if(this.bombType == 1){ this.StrengthenBulletNum += 1200; this.unschedule(this.addSingleBullet); this.schedule(this.addDoubleBullet,0.2); this.type = 2; this.bombType = 0; }else if(this.bombType == 2) { //全屏炸弹 //this.enemyLayer.allDestroy(); this.controlLayer.addBomb(); this.bombType = 0; } } }); BulletLayer.create = function () { var sg = new BulletLayer(); if (sg && sg.init()) { return sg; } return null; };
这代码应该算详细了吧。下面上几张图。。。
原文链接:https://www.f2er.com/cocos2dx/347063.html