用CocosCreator3.6开发一款拼图游戏(十九)

119 阅读2分钟

上次我们说到了游戏得分的功能,我们今天继续来完善它。废话少说,下面直接开始。

1. 打开game.ts脚本。增加一个函数setbestScore,专门用来处理最好得分的。也就是最短时间的。


    setbestScore(){

        let itemType=''

        if(this.gameType==0){

            itemType='itemJigsawContent'

        }else{

            itemType='item' 

        }

        let  strbest_score=itemType+'_'+this.itemNum

        let  bestScore =sys.localStorage.getItem(strbest_score)

        if(!bestScore){

            this.best_score.string='最佳:无' 

        }else{

           var str_bestScore= this.formatTime(bestScore)

               this.best_score.string= str_bestScore

        }

        console.log('最佳成绩'+this.best_score)

    }

2.(1)在initklotski函数尾端调用setbestScore方法。

        this.setbestScore();

image.png

(2)同样,在initjigsaw 函数尾端调用setbestScore方法

        this.setbestScore();

image.png

3.在checkSuccess函数里也要进行相对应的处理。


          if(this.callback){

                this.unschedule(this.callback);

            }

 
            let  strbest_score=itemType+'_'+this.itemNum

            let  bestScore =sys.localStorage.getItem(strbest_score)

            if(!bestScore){

                    sys.localStorage.setItem(strbest_score,String(this.timeNum))

                    bestScore=String(this.timeNum)

            }else{

                if( parseInt(bestScore) > this.timeNum){

                    bestScore=String(this.timeNum)

                    sys.localStorage.setItem(strbest_score,String(this.timeNum))

                }

            }

           console.log('最佳成绩2:'+bestScore)

          let str_bestScore=this.formatTime(bestScore);

            this.best_score.string='最佳'+str_bestScore

image.png

至此,分数功能就做完了。接下来,我们做一个游戏结束的界面。

  4. 在canvas下新建一个空节点,并且命名为gameOver

image.png

  5. (1)在gameOver下新建一个Button(按钮)

image.png

(2)并且命名为return_home

image.png

(3)Sting改为“返回首页”

image.png

(4)调整按钮大小。

image.png

(5)调整return_home 下的Label的位置和字体的大小。

image.png

(6)调整return_home的位置。

image.png

(7)return_hom的按钮过渡类型Transition改为SCALE

image.png

6. 同样在游戏结束的界面增加一个重玩的按钮。

(1)在gameOver节点下新增一个按钮,并且命名为re_play

image.png

(2)re_play下的Label的string改为重新开始.颜色设置为黑色。

image.png

字体的大小设置为40

image.png

(3)调整re_play的位置,大小设置为200X100

image.png

(4)re_play的按钮过渡类型Transition改为SCALE image.png

好了今天就到这里了,主要完善了分数的功能。以及添加了游戏结束界面,在游戏结束界面加来了两个按钮,下次我们继续来完善这两个按钮的功能。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家