【用Cocos Creator給暗戀的女生寫一個遊戲(13)】——整體回顧,工程文件

Load場景

這裏寫圖片描述

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;
        }
    },

});

RunGame場景

這裏寫圖片描述

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);
        }
    },
});

JumpGame場景

這裏寫圖片描述

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<this.cloudPre.length; i++){
            this.cloudPoolSet[i] = new cc.NodePool(require("Game2"));
            for(var j=0; j<7; j++){
                this.cloudPoolSet[i].put(cc.instantiate(this.cloudPre[i]));
            }
        }
        this.starPool = new cc.NodePool(require("Game2"));
        for(var m = 0; m < 10; m++){
            var star = cc.instantiate(this.starPre);
            this.starPool.put(star);
        }

        for(var n = 0;n < 7;n++){
            this.addPrefab();
        }
    },

    onCollisionEnter: function (other,self) {

        if(other.tag === 1 || other.tag === 2 || other.tag === 3){
                this.desPrefab(other.node,other.tag);
                this.addPrefab();
        }
        if(other.tag === 666){
            this.desPrefab(other.node,other.tag);
        }

    },

    addPrefab: function(){
        var cloud;
        var n;
        switch(this.game.level){
            case 0://stable
                cloud = this.cloudPoolSet[0].get();
                break;
            case 1://stable+once
                n = Math.round(Math.random()*1);
                cloud = this.cloudPoolSet[n].get();
                break;
            case 2://stable+once+move
                n = Math.round(Math.random()*2);
                cloud = this.cloudPoolSet[n].get();
                break;
            case 3:
                n = Math.round(Math.random()*2);
                cloud = this.cloudPoolSet[n].get();
                break;
            case 4:
                n = Math.round(Math.random()*2);
                cloud = this.cloudPoolSet[n].get();
                break;
            default:
                n = Math.round(Math.random()*2);
                cloud = this.cloudPoolSet[n].get();
                break;
        }
        if(cloud === null){
            return;
        }
        this.cloudLayer.addChild(cloud);
        cloud.x = -cc.winSize.width/2 + cloud.width/2+Math.random()*(cc.winSize.width- cloud.width);
        this.lastCloudY = cloud.y = this.lastCloudY + this.cloudSpace;
        //add star
        if(Math.random()<0.7){
            var star = this.starPool.get();
            if(star === null){
                return;
            }
            this.cloudLayer.addChild(star);
            star.x = -cc.winSize.width/2 + cloud.width/2+Math.random()*(cc.winSize.width- cloud.width);
            star.y = cloud.y+this.cloudSpace/2;

        }

    },

    desPrefab: function(node,tag){
        switch(tag){
            case 1:
                this.cloudPoolSet[0].put(node);
                break;
            case 2:
                this.cloudPoolSet[1].put(node);
                break;
            case 3:
                this.cloudPoolSet[2].put(node);
                break;
            case 666:
                this.starPool.put(node);
                break;
            default:break;
        }
    }

});

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;
        }
    },
});

一共808行代碼,不信你查。。。

如果你前面都跟着我做了,那麼你看到這裏的每一個場景,每一塊代碼都會有感情的,
我剛開始做遊戲的時候總是忘記函數名怎麼寫(現在也忘),所以我把所有的腳本文
件都羅列在這,給大家也給自己留個參考

打包apk文件:http://pan.baidu.com/s/1sltNOpJ
資源工程文件:http://pan.baidu.com/s/1eSBflPC

發佈了71 篇原創文章 · 獲贊 157 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章