Load.js
cc.Class({
extends: cc.Component,
properties: {
loadBar: cc.ProgressBar,
},
onLoad: function () {
var load = function(){
this.loadBar.progress += 0.1;
if(this.loadBar.progress > 0.9){
cc.director.loadScene("Menu");
}
}
this.schedule(load,0.8);
},
});
Record.js
cc.Class({
extends: cc.Component,
properties: {
bestRunScore: 0,
bestJumpScore: 0,
},
onLoad: function () {
cc.game.addPersistRootNode(this.node);
var bestRunScore = cc.sys.localStorage.getItem("bestRunScore");
if(bestRunScore){
this.bestRunScore = bestRunScore;
}
var bestJumpScore = cc.sys.localStorage.getItem("bestJumpScore");
if(bestRunScore){
this.bestJumpScore = bestJumpScore;
}
},
updateRunScore: function(score){
if(score > this.bestRunScore){
this.bestRunScore = score;
}
},
updateJumpScore: function(score){
if(score > this.bestJumpScore){
this.bestJumpScore = score;
}
},
save(){
cc.sys.localStorage.setItem('bestRunScore', this.bestRunScore);
cc.sys.localStorage.setItem('bestJumpScore', this.bestJumpScore);
},
});
Menu.js
cc.Class({
extends: cc.Component,
properties: {
tipsLabel:cc.Label,
tipsText:{
type:cc.String,
default:[],
},
runScore: cc.Label,
jumpScore: cc.Label,
bgm:cc.AudioClip,
},
onLoad: function () {
this.record = cc.find("Record").getComponent("Record");
this.runScore.string = "你最远跑了" + this.record.bestRunScore+ "m";
this.jumpScore.string = "你最高跳了" + this.record.bestJumpScore+ "m";
//返回键退出游戏
let self = this;
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
if(keyCode == cc.KEY.back){
self.record.save();
cc.director.end();
}
}
}, self.node);
var show = function(){
if(self.tipsText.length>0){
var n = Math.round(Math.random()*(self.tipsText.length-1));//随机选择tipsText数组中的一个
self.tipsLabel.string = self.tipsText[n];
}
}
show();
this.schedule(show,10);//每十秒更新一次Tips
this.isPlaying = false;
},
onBtnPlay1: function(){
cc.director.loadScene("RunGame");
},
onBtnPlay2: function(){
cc.director.loadScene("JumpGame");
},
onBtnShow(){
//原生平台audioSource.isPlaying暂时不支持
if(!this.isPlaying){
cc.audioEngine.playMusic(this.bgm);
this.isPlaying = true;
}else{
cc.audioEngine.stopMusic(this.bgm);
this.isPlaying = false;
}
},
});
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,
//-- 获取背景音效
gameBgAudio:cc.AudioClip,
//-- 获取失败音效
gameOverAudio:cc.AudioClip,
//-- 获取能量音效
scoreAudio: cc.AudioClip,
},
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);
cc.audioEngine.playMusic(this.gameBgAudio,true);
},
stopGame: function(){
cc.director.getCollisionManager().enabled = false;
cc.audioEngine.stopMusic(this.gameBgAudio);
cc.audioEngine.playMusic(this.gameOverAudio);
this.gameOverMenu.active = true;
this.overScore.string = this.score+"m";
cc.find("Record").getComponent("Record").updateRunScore(this.score);
},
gainScore: function(){
this.score += 10;
this.scoreLabel.string = this.score+"m";
},
gainEnergy:function(){
cc.audioEngine.playEffect(this.scoreAudio);
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");
},
});
CameraManager.js
cc.Class({
extends: cc.Component,
properties: {
far:cc.Node,
farRelSpeed:0,//相对主角移动速度
farOffX:0,//循环滚动距离
ground:cc.Node,
groundRelSpeed:0,
groundOffX:0,
pipeLayer:cc.Node,
layerRelSpeed:0,
},
init: function (game) {
this.game = game;
this.oFarX = this.far.x;
this.oGroundX = this.ground.x;
},
moveBg: function(distance){
this.far.x -= distance * this.farRelSpeed;
if(this.far.x < (this.oFarX - this.farOffX)){
this.far.x = this.oFarX;
}
this.ground.x -= distance * this.groundRelSpeed;
if(this.ground.x < (this.oGroundX-this.groundOffX)){
this.ground.x = this.oGroundX;
}
this.pipeLayer.x -= distance * this.layerRelSpeed;
},
});
PrefabManager.js
cc.Class({
extends: cc.Component,
properties: {
pipeGroupPre:cc.Prefab,
starPre:cc.Prefab,
pipeLayer:cc.Node,
//上下管子之间距离范围
spacingRange: cc.p(0,0),
// 下面管子Y轴偏移量范围
botYRange: cc.p(0,0),
// 左右管子之间距离
pipeSpace:0,
//第一个管子位置
oPipeX:0,
//星星位置范围
starYRange:cc.p(0,0),
},
init: function (game) {
this.game = game;
this.pipePool = new cc.NodePool();
this.starPool = new cc.NodePool();
for(var i=0;i<4;i++){
this.pipePool.put(cc.instantiate(this.pipeGroupPre));
this.starPool.put(cc.instantiate(this.starPre));
}
this.curPipeX = this.oPipeX;//当前管子位置
this.spawnPipe();
this.spawnPipe();
},
onCollisionEnter: function(other,self){
if(other.tag === 333){
this.desPipe(other.node);
}else if(other.tag === 666){
this.desStar(other.node);
}
},
spawnPipe: function(){
var pipeGroup = this.pipePool.get();
var pipeTop = pipeGroup.getChildByName("PipeTop");
var pipeBot = pipeGroup.getChildByName("PipeBot");
var botYPos = this.botYRange.x + Math.random() * (this.botYRange.y - this.botYRange.x);
var space = this.spacingRange.x + Math.random() * (this.spacingRange.y - this.spacingRange.x);
var topYPos = botYPos + space;
pipeTop.y = topYPos;
pipeBot.y = botYPos;
pipeGroup.x = this.curPipeX;
this.pipeLayer.addChild(pipeGroup);
this.spawnStar();
this.curPipeX += this.pipeSpace;
},
desPipe: function(node){
this.pipePool.put(node);
this.spawnPipe();
},
spawnStar: function(){
if(Math.random() < 0.8){
var star = this.starPool.get();
star.y = this.starYRange.x + Math.random()*(this.starYRange.y - this.starYRange.x);
star.x = this.curPipeX + this.pipeSpace/2;
this.pipeLayer.addChild(star);
}
},
desStar: function(node){
this.starPool.put(node);
},
});
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,//属性面板不显示
},
jumpAudio:cc.AudioClip,
dieAudio:cc.AudioClip,
superAudio:cc.AudioClip,
},
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(){
cc.audioEngine.playEffect(this.jumpAudio);
this.speedY = this.jumpSpeed;
},
strengthen:function(){
cc.audioEngine.playEffect(this.superAudio);
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(){
cc.audioEngine.playEffect(this.dieAudio);
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);
}
},
});
Game2.js
var PrefabManager = require("PrefabManager2");
var CameraManager = require("CameraManager2");
var Player = require("Player2");
cc.Class({
extends: cc.Component,
properties: {
player:Player,
prefabManager:PrefabManager,
cameraManager:CameraManager,
scoreText: cc.Label,
energyBar: cc.ProgressBar,
//结束菜单
gameOverMenu: cc.Node,
bgAudio:cc.AudioClip,
energyAudio:cc.AudioClip,
overAudio:cc.AudioClip,
},
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.gameOverMenu.active = false;
this.score = 0;
this.level = 0;
this.scoreText.string = "0m"
this.player.init(this);
this.cameraManager.init(this);
this.prefabManager.init(this);
cc.audioEngine.playMusic(this.bgAudio,true);
},
stopGame: function(){
cc.director.getCollisionManager().enabled = false;
cc.audioEngine.stopMusic(this.bgAudio);
cc.audioEngine.playMusic(this.overAudio);
this.gameOverMenu.getChildByName('OverScore').getComponent(cc.Label).string = this.score;
this.gameOverMenu.active = true;
//存储数据
cc.find("Record").getComponent("Record").updateJumpScore(this.score);
},
gainScore: function(scoreAdd) {
this.scoreText.string = (this.score += scoreAdd) + "m";
if(Math.round(this.score/200) > this.level){
this.level++;
if(this.level>5){
this.player.xSpeedMax += 50;
}
}
},
gainEnergy: function(){
cc.audioEngine.playEffect(this.energyAudio);
this.energyBar.progress += 0.1;
if(this.energyBar.progress >= 0.9){
this.player.superJump();
}
},
returnMenu(){
cc.director.loadScene("Menu");
},
});
CameraManager2.js
cc.Class({
extends: cc.Component,
properties: {
bgSky:cc.Node,
skySca:0,
bgHill:cc.Node,
hillSca:0,
bgHillnear:cc.Node,
hillnearSca:0,
bgFloor:cc.Node,
floorSca:0,
cloudLayer:cc.Node,
cloudSca:0,
camera: cc.Node,
player: cc.Node,
},
init: function(game){
this.game = game;
},
moveBg: function(distance){
if(this.player.y > this.camera.y+this.camera.height/2){
this.bgSky.y -= distance * this.skySca;
this.bgHill.y -= distance * this.hillSca;
this.bgHillnear.y -= distance * this.hillnearSca;
this.bgFloor.y -= distance * this.floorSca;
this.cloudLayer.y -= distance * this.cloudSca;
this.player.y -= distance;
this.game.gainScore(Math.round(distance/20));
}
}
});
PrefabManager2.js
cc.Class({
extends: cc.Component,
properties: {
cloudLayer: cc.Node,
player: cc.Node,
cloudPre:{
type:cc.Prefab,
default:[]
},
cloudSpace:0,
starPre: {
type:cc.Prefab,
default:null
},
},
init: function (game) {
this.game = game;
this.lastCloudY = -500;
this.cloudPoolSet = [];
for(var i = 0; i
Player2.js
var STATE = cc.Enum({
JUMP: 0,//原位置
DROP: 1,
DEAD: 2,
SUPERJUMP: 3,
});
cc.Class({
extends: cc.Component,
properties: {
xSpeedMax: 0,
jumpSpeed: 0,
gravity: 0,
groundY: 0,
jumpAudio:cc.AudioClip,
superAudio:cc.AudioClip,
},
init: function (game) {
this.game = game;
this.speedX = 0;
this.speedY = this.jumpSpeed;
this.botPos = this.groundY;
this.moveFlag = true;
this.state = STATE.JUMP;
let self = this;
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event) {
var touchLoc = touch.getLocation();
if (touchLoc.x >= cc.winSize.width/2) {
self.speedX = self.xSpeedMax;
self.node.scaleX = 1;
} else {
self.speedX = -self.xSpeedMax;
self.node.scaleX = -1;
}
return true;
},
onTouchEnded: function(touch, event) {
//self.speedX = 0;
}
}, self.node);
//键盘测试按钮
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
switch(keyCode) {
case cc.KEY.left:
self.node.scaleX = -1;
self.speedX = -self.xSpeedMax;
break;
case cc.KEY.right:
self.node.scaleX = 1;
self.speedX = self.xSpeedMax;
break;
}
},
onKeyReleased: function(keyCode, event) {
//self.speedX = 0;
}
}, self.node);
this.superJump();
},
onCollisionEnter: function (other,self) {
//CameraManager屏幕外下方的回收线
if(other.tag === 444){
this.state = STATE.DEAD;
this.node.color = cc.Color.BLACK;
this.game.stopGame();
}
if(other.tag === 666){
this.game.prefabManager.desPrefab(other.node,other.tag);
this.game.gainEnergy();
}else{
if(this.state === STATE.DROP){
this.jump();
if(other.tag === 2){
this.game.prefabManager.desPrefab(other.node,other.tag);
this.game.prefabManager.addPrefab();
}
}
}
},
jump: function(){
this.state = STATE.JUMP;
this.speedY = this.jumpSpeed;
cc.audioEngine.playEffect(this.jumpAudio);
},
superJump: function(){
this.state = STATE.SUPERJUMP;
this.node.color = cc.Color.RED;
this.speedY = this.jumpSpeed*2;
cc.audioEngine.playEffect(this.superAudio);
},
update: function (dt) {
if(this.state === STATE.DEAD){
return;
}
//更新水平位置
this.node.x += this.speedX * dt;
if(this.node.x < -cc.winSize.width/2){
this.node.x = cc.winSize.width/2;
}
if(this.node.x > cc.winSize.width/2){
this.node.x = -cc.winSize.width/2;
}
//更新Y轴位置
this.speedY -= this.gravity * dt;
this.node.y += this.speedY * dt;
//更新状态
if(this.speedY < 0){
//超级状态上升时没有吃到star就变为普通状态
if(this.state == STATE.SUPERJUMP){
this.game.energyBar.progress = 0;
this.node.color = cc.Color.WHITE;
}
this.state = STATE.DROP;
}
//更新镜头位置
this.game.cameraManager.moveBg(this.speedY * dt);
},
});
MoveCloud.js
cc.Class({
extends: cc.Component,
properties: {
moveSpeed: 0,
},
update: function (dt) {
this.node.x += this.moveSpeed * dt;
if(this.node.x < -cc.winSize.width/2+this.node.width/2 || this.node.x > cc.winSize.width/2-this.node.width/2){
this.moveSpeed = -this.moveSpeed;
this.node.scaleX = -this.node.scaleX;
}
},
});
如果你前面都跟着我做了,那么你看到这里的每一个场景,每一块代码都会有感情的,
我刚开始做游戏的时候总是忘记函数名怎么写(现在也忘),所以我把所有的脚本文
件都罗列在这,给大家也给自己留个参考
打包apk文件:http://pan.baidu.com/s/1sltNOpJ
资源工程文件:http://pan.baidu.com/s/1eSBflPC