码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-06-10 09:28:40      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

游戏结束界面:

bubuko.com,布布扣

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

bubuko.com,布布扣
/**
 * 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;
};
bubuko.com,布布扣
  这里游戏数据的缓存我们是使用本地缓存localStorage来做的
bubuko.com,布布扣
       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);
bubuko.com,布布扣

  首先,我们先判断是否存在本体缓存数据,不存在的话我们就添加缓存数据并且把当前的游戏时间通过 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做的消除类游戏《英雄爱消除》(4)——游戏结束,布布扣,bubuko.com

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

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/jackyWHJ/p/3778424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!