上一贴地址: http://www.cocoachina.com/bbs/read.php?tid=196128&page=1&toread=1#tpc
- Enemy
敌机
在“MyGameLayer”的顶部:
_targets:null, // 设定敌机的全局变量
加进init:function(){}:
this._targets= ]; // 敌机数组
this.schedule(this.addTarget,0.5); // 每0.5秒更新一次方法addTarget
加在“addBullets:function(){}”之后
addTarget:function(){
var target = cc.Sprite.create(s_Jet); // 创建并载入敌机(图片和我放飞机一样)
target.setScale(0.33); // 设置尺寸比例
target.setRotation(180); // 设置角度,旋转180度
var minRange = target.getContentSize().width / 2; // 获得最小移动范围:图片尺寸的二分之一
var maxRange = this._size.width -target.getContentSize().width / 2; // 获得最大移动范围:屏幕宽度减去图片尺寸的二分之一
var showUpPosition = Math.random() * this._size.width; // 随机出现地点
var minDuration = 2.5;
var maxDuration = 10.0;
var differentDuration = maxDuration - minDuration;
var actualDuration = Math.random() * differentDuration +minDuration; //实际飞行时间
target.setPosition(cc.p(showUpPosition,this._size.height)); // 设置敌机出现地点
var actionMove = cc.MoveTo.create(actualDuration,cc.p(showUpPosition, 0 - target.getContentSize().height));
var actionMoveDone =cc.CallFunc.create(this.spriteMoveFinished, this);
target.runAction(cc.Sequence.create(actionMove,actionMoveDone));
target.setTag(1);
this._targets.push(target);
this.gameLayer02.addChild(target, 0);
}
Kamikaze
- Enemy bullet
敌机子弹
我们的敌人并不是神风敢死队。他们也会向你射击。所以:
加在“MyGameLayer”的顶部:
_enemyBullet:null,
加在“init:function(){}”的里面:
this._enemyBullet = ]; // 创建敌机子弹数组
加进“addTarget(){}”
var enemyBullet = cc.Sprite.create(s_bullets, cc.rect(0,50, 33, 70));
enemyBullet.setRotation(180);
var i;
for(i in this._targets) { // 遍历敌机数组,获取敌机位置,并赋值给敌机子弹
var targetThisOne = this._targets;
var targetPosition = targetThisOne.getPosition();
enemyBullet.setPosition(targetPosition.x,targetPosition.y);
}
var enemyBulletDuration = 2; // 子弹时间
var enemyBulletTime = enemyBulletDuration *(targetPosition.y / this._size.height); // 实际子弹时间
var ebActionMoveTo = cc.MoveTo.create(enemyBulletTime,cc.p(showUpPosition, -enemyBullet.getContentSize().height));
var ebActionMoveDone =cc.CallFunc.create(this.spriteMoveFinished, this);
enemyBullet.runAction(cc.Sequence.create(ebActionMoveTo,ebActionMoveDone)); // 子弹动作
enemyBullet.setTag(5); // 设置子弹标签为5
this._enemyBullet.push(enemyBullet); // 推入数组中
this.gameLayer02.addChild(enemyBullet, 1); // 将子弹载入层中
}
为了清理掉敌人和敌人的子弹,所以我们对“spriteMoveFinished:function(sprite){}”做出一些改变。让这个方法变得更加复杂。
spriteMoveFinished:function(sprite){ // 精灵完成动作后将其移除
this.gameLayer02.removeChild(sprite, true);
if(sprite.getTag == 6){ // 如果目标的标签为6,则遍历子弹的数组,将里面的内容删除
var index01 = this._bullets.indexOf(sprite);
if(index01 > -1) {
this._bullets.splice(index01, 1);
} else if(sprite.getTag == 1){ // 如果目标标签为1,则遍历敌机数组,将里面的内容删除
var index02 = this._targets.indexOf(sprite);
if(index02 > -1) {
this._targets.splice(index02, 1);
}
} else if(sprite.getTag == 5){ // 如果目标标签为5,则遍历敌机子弹数组,将里面的内容删除
var index03 = this._enemyBullet.indexOf(sprite);
if(index03 > -1) {
this._enemyBullet.splice(index03, 1);
}
}
}
},
Across through
子弹和飞机会穿过对方。他们并不会损毁。所以我们要编写“碰撞检测”的代码。
- Collision detection:
碰撞检测
在init:function(){}的里面:
this.schedule(this.updateGame); // 更新updateGame方法(未填入时间,则默认和游戏帧数频率相同)
加在addTarget(){}的后面:
updateGame: function(){
var targets2Delete = ]; // 添加敌机删除数组,相撞的会被存储在这,之后删除
var jetFighterRect = this._jetSprite.getBoundingBox();
var i;
var enemyBulletsDelete = ]; // 添加敌机子弹数组,相撞的会被存储在这,之后删除
// 与敌机子弹碰撞,消除敌机子弹
for(i in this._enemyBullet){ // 遍历敌机子弹
var enemyBulletsKill = this._enemyBullet; // 获取数组中的第i个子弹
var enemyBulletRect = enemyBulletsKill.getBoundingBox(); // 获得相应子弹的图片边界
if (cc.rectIntersectsRect(enemyBulletRect,jetFighterRect)){ // 判断敌机子弹图片是否和我方战机图片有交集的部分
enemyBulletsDelete.push(enemyBulletsKill); // 将有交集的子弹送入敌机子弹删除数组
}
for(i in enemyBulletsDelete){ // 将敌机子弹删除数组中的内容删除
var enemyBulletRemove = enemyBulletsDelete;
var index =this._enemyBullet.indexOf(enemyBulletRemove);
if(index > -1){
this._enemyBullet.splice(index, 1);
}
this.gameLayer02.removeChild(enemyBulletRemove);
}
enemyBulletsKill = null;
}
// 敌机与我方子弹|| 飞机碰撞,消除我方子弹|| 飞机和敌机
for(i in this._targets){ // 遍历所有敌机
var target = this._targets; // 获取第i个敌机
var targetRect = target.getBoundingBox(); // 获取相应敌机的图片边界
var bullets2Delete = ]; // 我方子弹删除数组
var targetRemove = ]; // 敌机删除数组
if (cc.rectIntersectsRect(targetRect, jetFighterRect)){ // 判断敌机和我方战机是否有交集
targetRemove.push(target); // 将有交集的敌机送入数组中
}
for(i in targetRemove){ // 遍历删除数组中的内容并删除
var targetAway = targetRemove;
var index = this._targets.indexOf(targetAway);
if(index > -1){
this._targets.splice(index, 1);
}
this.gameLayer02.removeChild(targetAway);
}
for(i in this._bullets){ // 子弹和敌机的碰撞检测,原理同上
var bullet = this._bullets;
var bulletRect = bullet.getBoundingBox();
if(cc.rectIntersectsRect(bulletRect, targetRect)){
bullets2Delete.push(bullet);
}
}
if(bullets2Delete.length > 0){
targets2Delete.push(target);
}
for(i in bullets2Delete){
var bullet = bullets2Delete;
var index = this._bullets.indexOf(bullet);
if(index > -1){
this._bullets.splice(index, 1);
}
this.gameLayer02.removeChild(bullet);
}
bullets2Delete = null;
}
for(i in targets2Delete){
var target = targets2Delete;
var index = this._targets.indexOf(target);
if(index > -1) {
this._targets.splice(index, 1);
}
this.gameLayer02.removeChild(target);
}
targets2Delete = null;
},
Killthe enemy
- Life& Score
生命值 & 计分
在”MyGameLayer”的顶部:
lives01:null, // 设定为全局变量
number:null, // 设定为全局变量
score:0, // 设定为全局变量,并赋值为0
scoreBoard:null, // 设定为全局变量
inside init:function(){}:
this.score = 0;
this.scoreBoard =cc.LabelTTF.create(this.score,"Impact", 28); // 创建这个字符,设置了字体和大小
this.scoreBoard.setPosition(3 * this._size.width / 4,this._size.height - 40); // 设置字体
this.gameLayer02.addChild(this.scoreBoard, 6); // 加载进层
this.number = 10; // 设立生命值为10
this.lives01 = cc.LabelTTF.create(this.number,"Impact", 28);
this.lives01.setPosition(this._size.width / 4,this._size.height - 40);
this.gameLayer02.addChild(this.lives01, 5);
在Update()之后添加
reduceLives:function(){
this.number -= 1; // 调用这个方法时number数减1
this.lives01.setString(this.number); // 更新生命值数值
}
在“Update:function(){}:”里,更新方法:
if (cc.rectIntersectsRect(enemyBulletRect,jetFighterRect)){ // 判断如果和敌机子弹相撞则减少生命值,并推入删除数组
this.reduceLives();
enemyBulletsDelete.push(enemyBulletsKill);
}
if (cc.rectIntersectsRect(targetRect, jetFighterRect)){ // 判断如果敌机和我方战机相撞,则减少生命值,并推入删除数组
this.reduceLives();
targetRemove.push(target);
}
for(i in this._bullets){
var bullet = this._bullets;
var bulletRect = bullet.getBoundingBox();
if(cc.rectIntersectsRect(bulletRect, targetRect)){
bullets2Delete.push(bullet);
this.score += 100; // 每干掉一架敌机则加上100分
this.scoreBoard.setString(this.score); // 更新数值
}
}
Lives & Score
我们遇到麻烦了。即使生命值非常的低,我们的飞机仍然在飞。我们现在就要让他停下来。

Low on lives
更新方法:reduceLives:function(){}
reduceLives:function(){
this.number -= 1;
this.lives01.setString(this.number);
if (this.number == 0){
var gameOverScene = GameOverScene.create();// 创建结束场景
cc.Director.getInstance().replaceScene(cc.TransitionProgressRadialCCW.create(1.2,gameOverScene)); // 场景转换代码
}
},
- Ending Scene
结束场景
创建一个新的“JS”文件用来创建场景(结束场景)。

命名为:GameOverScen
添加进“Cococs2d.js”

创建一个新的图层:
var GameOverLayer = cc.LayerColor.extend({
init:function(){
this._super();
this.setColor(cc.c4(126, 126, 126, 126));
var winSize = cc.Director.getInstance().getWinSize();
var _label = cc.LabelTTF.create("GameOver","Arial", 60);
_label.setPosition(cc.p(winSize.width / 2,winSize.height / 2));
this.addChild(_label);
return true;
}
})
GameOverLayer.create = function(){
var gameOverLayer = new GameOverLayer;
if(gameOverLayer && gameOverLayer.init()){
return gameOverLayer;
}
return null;
}
var GameOverScene = cc.Scene.extend({
_layer:null,
init:function(){
this._layer = GameOverLayer.create();
this.addChild(this._layer);
return true;
}
})
GameOverScene.create = function(){
var scene = new GameOverScene;
if(scene && scene.init()){
return scene;
}
return null;
}
这些代码并没有涉及到新的东西。。。。。。
Ending Scene
13) Background music:
背景音乐:
最后一部分:添加背景音乐。
Inside init:function(){}:
在init:function(){}:里面:
var audioEngine = cc.AudioEngine.getInstance();
audioEngine.playMusic("res/Sounds/background.mp3",true);
Addinto onNewGame:function(){}
添加进方法:onNewGame:function(){}
var musicStop = cc.AudioEngine.getInstance();
musicStop.stopMusic("res/Sounds/background.mp3");
Add intoonNewGame:function(){}
添加进方法:onNewGame:function(){}
var anotherMusicPlay = cc.AudioEngine.getInstance();
anotherMusicPlay.playMusic("res/Sounds/BGM.mp3",true);
Add into reduceLives:function(){}
添加进方法:reduceLives:function(){}
reduceLives:function(){
if(```){
var musicStop = cc.AudioEngine.getInstance();
musicStop.stopMusic("res/Sounds/BGM.mp3");
}
}



