至此爲止,我們已經把主要的技術問題都解決了,現在我們給遊戲添加
具體的邏輯
給主角添加碰撞組件
回顧一下我們已經添加的碰撞組件的節點
管子兄弟:tag=333
管子哥哥和管子弟弟:tag=3331
大老公:tag=666
主角可以跟它們產生碰撞,碰撞的效果如下
管子兄弟:(離開時)加分(相當於已經跳過了一組管子)
管子哥哥或者管子弟弟:主角死亡,遊戲失敗
大老公:能量槽加1,大老公銷燬
完善主角邏輯,重新修改如下
Player.js
var STATE = cc.Enum({
NONE:0,
NORMAL:1,
SUPER:2,
DEAD:3,
});
cc.Class({
extends: cc.Component,
properties: {
oSpeedX:0,
superSpeedX:0,
gravity:0,
jumpSpeed:0,
groundY:0,
state:{
default:STATE.NONE,
type:STATE,
visible:false,//屬性面板不顯示
}
},
init: function (game) {
this.game = game;
this.speedY = 0;
this.speedX = this.oSpeedX;
this.state = STATE.NORMAL;
this.registerInput();
//cc.director.getCollisionManager().enabled = true;
},
onCollisionEnter:function(other,self){
if(this.state == STATE.NORMAL){
if(other.tag == 666){
this.game.gainEnergy();
this.game.prefabManager.desStar(other.node);
}
if(other.tag == 3331){
this.die();
}
}
},
onCollisionExit:function(other,self){
if(other.tag == 333){
this.game.gainScore();
}
},
registerInput: function(){
let self = this;
//鍵盤事件
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
if(keyCode == cc.KEY.back){
cc.director.loadScene("Menu");
}else{
self.jump();
}
}
}, self.node);
//觸摸事件
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event) {
self.jump();
}
}, self.node);
},
jump:function(){
this.speedY = this.jumpSpeed;
},
strengthen:function(){
this.state = STATE.SUPER;
this.node.color = cc.Color.RED;
this.speedX = this.superSpeedX;
//5秒超級速度,2秒普通速度進行緩衝,一共7秒無敵
let self = this;
var cache = function(){
self.speedX = self.oSpeedX;
}
this.scheduleOnce(cache,5);
this.scheduleOnce(this.recover,7);
},
recover:function(){
this.state = STATE.NORMAL;
this.node.color = cc.Color.WHITE;
this.oSpeedX += 10;//給遊戲加一點難度
this.speedX = this.oSpeedX;
this.game.energyBar.progress = 0;
this.game.energyBar.node.getChildByName("Bar").color = cc.Color.GREEN;
},
die:function(){
this.state = STATE.DEAD;
this.node.color = cc.Color.BLACK;
this.game.stopGame();
},
update: function (dt) {
if(this.state != STATE.NONE && this.state != STATE.DEAD){
this.speedY -= this.gravity * dt;
this.node.y += this.speedY * dt;
if(this.node.y <= this.groundY){
this.node.y = this.groundY;
}
this.game.cameraManager.moveBg(this.speedX * dt);
}
},
});
Game.js
var Player = require("Player");
var CameraManager = require("CameraManager");
var PrefabManager = require("PrefabManager");
cc.Class({
extends: cc.Component,
properties: {
player:Player,
cameraManager:CameraManager,
prefabManager:PrefabManager,
energyBar:cc.ProgressBar,
scoreLabel:cc.Label,
gameOverMenu:cc.Node,
overScore:cc.Label,
},
onLoad: function () {
//返回鍵返回菜單
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
if(keyCode == cc.KEY.back){
cc.director.loadScene('Menu');
}
}
}, this.node);
this.startGame();
},
startGame: function(){
cc.director.getCollisionManager().enabled = true;
this.energyBar.progress = 0;
this.score = 0;
this.scoreLabel.string = "0m"
this.cameraManager.init(this);
this.prefabManager.init(this);
this.player.init(this);
},
stopGame: function(){
cc.director.getCollisionManager().enabled = false;
this.gameOverMenu.active = true;
this.overScore.string = this.score+"m";
},
gainScore: function(){
this.score += 10;
this.scoreLabel.string = this.score+"m";
},
gainEnergy:function(){
this.energyBar.progress += 0.1;
if(this.energyBar.progress > 0.9){
this.energyBar.node.getChildByName("Bar").color = cc.Color.RED;
this.player.strengthen();
}
},
restartGame: function(){
cc.director.loadScene("RunGame");
},
returnMenu: function(){
cc.director.loadScene("Menu");
},
});
最終效果
結束菜單
至此,我們的第一個小遊戲就結束了