版权声明:本博客属个人原创,转载请注明。 https://blog.csdn.net/qq_33198758/article/details/82261258
1. 创建GameOver场景
在gamescence场景下新建一个gameover节点,如图1所示。在gameover节点下加入游戏结束背景、当前的分、在玩一次、回到首页、炫耀等游戏资源图片,如下图2所示。
2.GameOver实现
第一步:
在Game脚本的属性中加入gameOverNode节点,并且绑定gameover节点。加入bgNode节点,并和游戏背景绑定:
gameOverNode: {//游戏结束节点
default: null,
type: cc.Node
},
bgNode: {//游戏背景
default: null,
type: cc.Node
},
第一步:
由于游戏时这个节点是不可见的,所以在Game脚本的onLoad()函数中失能该节点。
this.gameOverNode.active = false;
第三步:
gameover函数实现,主要是使能gameover节点,显示当前得分,改变游戏背景的透明度:
gameOver: function () {//gameOver函数
this.gameOverNode.active = true; // 使能gameOverNode节点
this.isRunning = false;//游戏运行标志
this.currentScore.string='当前得分:'+ this.score.toString();//当前得分
this.bgNode.opacity=150;//改变游戏背景的透明度
},
第四步:
在NMagnetObstacle脚本onLoad()中设置一个pass标志,因为越过player以后,只加一分,所以需要此状态标志,否则在碰撞状态下回一直加分。
所以碰撞检测函数为:
onCollisionEnter: function(other,self){
if(other.tag === 0){
this.game.desNMagnetObstacle(this.node);
this.pass=false;//
}
if(other.tag === 1){
if(this.game.N){//得分逻辑
if(this.pass){
return;//表示已经加过分了,直接返回
}
else{
this.game.gainScore();//得分
this.pass=true;//改变状态
}
}
else{//游戏结束
this.game.gameOver();
this.node.destroy();
}
}
},
SMagnetObstacle脚本中的碰撞检测函数为:
onCollisionEnter: function(other,self){
if(other.tag === 0){
this.game.desSMagnetObstacle(this.node);
this.pass=false;
}
if(other.tag === 1){
if(this.game.N){
//gameoverSS
this.game.gameOver();
this.node.destroy();
}
else{//得分
if(this.pass){
return;
}
else{
this.game.gainScore();
this.pass=true;
}
}
}
},
3.返回游戏与回到首页
给游戏结束图片绑定backToFirstScence()点击事件,具体方法参见。游戏结束后,点击回到首页:
backToFirstScence:function(){//回到首页
this.gameOverNode.active = false; //失能游戏结束节点
cc.director.loadScene("startscence");//回到首页
},
游戏结束后,点击返回游戏:
onStartGame: function () {//重新开始游戏
this.gameOverNode.active = false;
this.bgNode.opacity=255;
this.MagnetSpeed=1;
// this.scoreDisplay.opacity=255;
// 初始化计分
this.resetScore();
// set game state to running
this.isRunning = true;
this.timer=0;
this.currentY=0;
cc.audioEngine.playEffect(this.bgmAudio, true);
this.spawnNMagnetPlayer();
this.spawnNMagnetObstacle(cc.p(this.node.width/6,600));
this.spawnSMagnetObstacle(cc.p(this.node.width/6,680));
this.spawnNMagnetObstacle(cc.p(this.node.width*5/6,500));
this.spawnNMagnetObstacle(cc.p(this.node.width*5/6,400));
this.spawnSMagnetObstacle(cc.p(this.node.width*5/6,800));
},