当前位置:网站首页>Wechat games (3)

Wechat games (3)

2022-06-22 07:25:00 qq_ forty-five million nine hundred and sixty-nine thousand fiv

One 、 Task content

The main content of this time is the script of the main body of the game , As the key part .

Two 、 Specific contents and key codes

1. initial

    onLoad() {
    
        this.success = false;
        //  Initial score 
        this.scoreNum = 0;
    },
    start () {
    
        let countDownLabel = cc.find("Canvas/bg_sprite/count_down").getComponent(cc.Label);
        countDownLabel.string = this.time + "s";
        this.schedule(function () {
    
            this.time --;
            countDownLabel.string = this.time + "s";
            if (this.time == 0) {
    
                cc.log(" Game over !");
                //  Get pop-up node 
                let resultNode = cc.find("Canvas/result");
                //  obtain title and content Two nodes 
                let titleNode = resultNode.getChildByName("title");
                let contentNode = resultNode.getChildByName("content");
                //  Show the score 
                titleNode.getComponent(cc.Label).string = " The final score  " + this.scoreNum;
                
                //  Get components 
                let contentLabel = contentNode.getComponent(cc.Label);
                switch (true) {
    
                    case this.scoreNum <= 3:
                        contentLabel.string = " Bull set bronze ";
                        break;
                    case this.scoreNum < 6:
                        contentLabel.string = " A bull trap expert ";
                        break;
                    case this.scoreNum >= 6:
                        contentLabel.string = " The king of cattle ";
                        break;

                }
                resultNode.active = true;
                cc.director.pause();

            }

        },1);

    },

2. Capture button click events

Set rope

 clickCapture:function (event,customEventDate) {
    
        this.rope_node.active = true;
        //  Set the order of the rope at the current parent node 
        this.rope_node.setSiblingIndex(100);

        //  Set the starting position of the rope 
        this.rope_node.y = -480;
        //  Move up 
        const up = cc.moveTo(0.5,this.rope_node.x,60);

Set the judgment result

   let result =  cc.callFunc(function () {
    
            //  Get the current cow's x spot 
            let currentX = this.cow_ins.x;
            if (currentX > -100 && currentX < 100){
    
                cc.log(" Capture success !");
                //  remove 
                let bgNode = this.node.getChildByName("bg_sprite");
                bgNode.removeChild(this.cow_ins);
                //  Get the type of cow 
                let ropeType = this.cow_ins.getComponent("cow").randomType +1;
                this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[ropeType];
                //  Generate a new cow node 
                this.cow_ins = cc.instantiate(this.cow_prefab);
                this.cow_ins.y = 0;
                bgNode.addChild(this.cow_ins);
                //
                this.success = true;
                //  fraction +1
                this.scoreNum ++;
            } else {
    
                cc.log(" Capture failed !")
            }

        },this);

Determine whether the capture is successful

let finish = cc.callFunc(function () {
    
            this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[0];
            //  Determine whether the capture is successful 
            if (this.success == true) {
    
                let  scoreLabel = cc.find("Canvas/bg_sprite/score").getComponent(cc.Label);
                scoreLabel.string = "Score:" + this.scoreNum;
                this.success = false ;
            }
        },this)

close button , Keep playing

  closeBtn() {
    
        cc.log(" Keep playing ");
        cc.director.resume();
        cc.director.loadScene("game");
    }

3、 ... and 、 summary

The game script is basically completed , Combined with the rest of the work, the game will be basically completed .

原网站

版权声明
本文为[qq_ forty-five million nine hundred and sixty-nine thousand fiv]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220717147632.html