从0开始用CocosCreator开发兔了个兔消消乐小游戏(七)

86 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第32天,点击查看活动详情

上次我们说到了添加输出的按钮,但功能还没有完善,今天我们继续。

1.为了方便显示提示,添加一个函数,专门用来显示提示信息的

(1)在Canvas 下新建一个空节点nodeTip image.png

(2)在节点nodeTip下再增加一个Sprite(精灵),并且命名为bg image.png

(3)调整节点bg大小 image.png

(4)设置bg的颜色和透明度 image.png

(5)节点nodeTip下再增加一个Label(文本) image.png

(6)调整字的大小 image.png

2.打开game.ts脚本

(1)添加装饰器

   @property({type:Node})

    nodeTip=null

(2)关联节点 image.png

(3)回到game.ts脚本,增加一个init函数

    init(){

        this.nodeTip.scale =new Vec3(0,0,0)

    }

(4)在start函数里调用init函数

      this.init()

image.png

(5) 增加显示提示的函数

    //显示提示

    showTip(str){

        this.nodeTip.getChildByName('Label').getComponent(Label).string =str

        tween(this.nodeTip)

        .to(0.1,{scale:new Vec3(1,1,1)})

        .delay(1)

        .to(0.1,{scale: new Vec3(0,0,0)})

        .start()

    }

(6)注意,要引入Label,tween image.png

3.输出元素块坐标

(1)添加一个函数,专门用来处理元素块的坐标输出,这样当点击按钮的时候就可以将坐标输出到浏器的console上了

    //输出元素块的坐标

    shuChuPosBlocks(){

        let str_pos=''

        let children =this.parentBlocks.children

        for (let i = 0; i < children.length; i++) {

            let v3_block=children[i].getPosition()

            str_pos=str_pos+'{x:'+v3_block.x+','+'y:'+v3_block.y+'},'  

        }

        if(children.length %3 ==0){

            if(children.length>0){

                console.log(str_pos)

            }else{

                console.log('块的个数为0,无效')

                this.showTip('块的个数为0,无效')

            }

        }else{

            console.log('块的个数不是3的倍数,无效')

            this.showTip('块的个数不是3的倍数,无效')

        }

    }

(2)在按钮处理事件函数上进行调用

     this.shuChuPosBlocks()

image.png

(3)输出效果 image.png

4. 在输出的时候作了一个判断,元素块必须是3的倍数才能输出,但我们现在只能添加元素块,为了方便,做一个可以删除元素块,就是增加一个按钮,通过切换不同的模式来进行元素增加或者删除

(1)在节点parentEdit下新增一个按钮,并且命名为btn_jia image.png

(2)调整按钮的位置 image.png

(3)按钮下的Label的Sring设置为加 image.png

(4)按钮的过渡类型设置为SCALE image.png

(5)添加点击事件,并且关联到callBackBtn函数,增加标识btn_jia image.png

今天就到这里了,主要说了元素块的坐标的输出,但还不是很完善,下次我们继续。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家