用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束

游戏结束界面:

用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束

在前面几个教程中,这个界面的创作所需要的知识点基本我们都讲过了,这里就说下用户数据的缓存吧,也是先来看下源码

/**

 * Power by  html5中文网(html5china.com)

 * author: jackyWHJ

 */

var GameOver = cc.Layer.extend({

    init:function () {

        var bRet = false;

        if (this._super()) {

            var sp = cc.Sprite.create(s_b05);

            sp.setAnchorPoint( cc.p(0,0) );

            this.addChild(sp, 0, 1);



            var logo = cc.Sprite.create(s_gameOver);

            logo.setAnchorPoint(cc.p(0,0));

            logo.setPosition(20,350);

            this.addChild(logo,10,1);



            var playAgainNormal = cc.Sprite.create(s_menu, cc.rect(378, 0, 126, 33));

            var playAgainSelected = cc.Sprite.create(s_menu, cc.rect(378, 33, 126, 33));

            var playAgainDisabled = cc.Sprite.create(s_menu, cc.rect(378, 33 * 2, 126, 33));



            var playAgain = cc.MenuItemSprite.create(playAgainNormal, playAgainSelected, playAgainDisabled, function(){

                flareEffect(this,this,this.onPlayAgain);

            }.bind(this) );



            var menu = cc.Menu.create(playAgain);

            this.addChild(menu, 1, 2);

            menu.setPosition(winSize.width / 2, 280);



            if(!localStorage.getItem("bestScore")){

                localStorage.setItem("bestScore",g_sharedGameLayer._timeString);

            };

            var lbScore = cc.LabelTTF.create("TIME "+g_sharedGameLayer._timeString,"Forte",26);

            lbScore.setPosition(winSize.width / 2,200);

            lbScore.setColor(cc.c3b(200,38,12));

//            lbScore.enableStroke(cc.c3b(0,0,0), 26);

            this.addChild(lbScore,10);



            var bestString = localStorage.getItem("bestScore");

            var bestArr = bestString.split(":");

            var sTime = bestArr[0]*60 + bestArr[1];

            if(sTime > g_sharedGameLayer._time){

                localStorage.setItem("bestScore",g_sharedGameLayer._timeString);

            }

            var bestScore = cc.LabelTTF.create("BEST TIME "+ localStorage.getItem("bestScore"),"Forte",26);

            bestScore.setPosition(180,150);

            bestScore.setColor(cc.c3b(200,38,12));

            this.addChild(bestScore,10);



            if(LLK.SOUND){

                cc.AudioEngine.getInstance().playMusic(s_mainMainMusic_mp3);

            }

            bRet = true;

        }

        return bRet;

    },

    onPlayAgain:function (pSender) {

        var scene = cc.Scene.create();

        scene.addChild(GameLayer.create());

        scene.addChild(GameControlMenu.create());

        cc.Director.getInstance().replaceScene(cc.TransitionFade.create(1.2,scene));

    }

});



GameOver.create = function () {

    var sg = new GameOver();

    if (sg && sg.init()) {

        return sg;

    }

    return null;

};



GameOver.scene = function () {

    var scene = cc.Scene.create();

    var layer = GameOver.create();

    scene.addChild(layer);

    return scene;

};
  这里游戏数据的缓存我们是使用本地缓存localStorage来做的
       if(!localStorage.getItem("bestScore")){

                localStorage.setItem("bestScore",g_sharedGameLayer._timeString);

            };

            var lbScore = cc.LabelTTF.create("TIME "+g_sharedGameLayer._timeString,"Forte",26);

            lbScore.setPosition(winSize.width / 2,200);

            lbScore.setColor(cc.c3b(200,38,12));

//            lbScore.enableStroke(cc.c3b(0,0,0), 26);

            this.addChild(lbScore,10);

  首先,我们先判断是否存在本体缓存数据,不存在的话我们就添加缓存数据并且把当前的游戏时间通过 cc.LabelTTF添加到Layer上。所过存在缓存数据,我们则取出缓存数据并且与当前游戏进行比较,然后缓存最好成绩并添加到Layer上

       var bestString = localStorage.getItem("bestScore");

            var bestArr = bestString.split(":");

            var sTime = bestArr[0]*60 + bestArr[1];

            if(sTime > g_sharedGameLayer._time){

                localStorage.setItem("bestScore",g_sharedGameLayer._timeString);

            }

 

 

你可能感兴趣的:(cocos2d-html5)