1. 首页
  2. 技术
  3. 后台

【打地鼠】

mouse.js

// Email: xinxin-de@qq.com
cc.Class({
    extends: cc.Component,
    properties: {
        
    },

    start () {

    },
    
    appear () {
        this.node.opacity = 255;
        this.node.y = -65;

        let seq = cc.sequence(cc.moveTo(0.2, cc.v2(0, 32)), cc.delayTime(0.5,), cc.moveTo(0.2, cc.v2(0, -65)));
        this.node.runAction(seq);
    },
});

game.js

// Email: xinxin-de@qq.com
cc.Class({
    extends: cc.Component,

    properties: {
        mouse: [cc.Node],
        hammer: cc.Node,
        txtCount: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;

        this.count = 0;
        this.txtCount.string = this.count;
    },

    start () {
        cc.director.getScheduler().schedule(this.logic, this, 0.5, false);

        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            for (let i = 0; i < this.mouse.length; i++) {
                if (this.mouse[i].opacity == 0) {
                    continue;
                }

                let parent = this.mouse[i].parent;
                let maskPos = parent.parent.convertToWorldSpaceAR(parent.position);
                let maskRect = cc.rect(maskPos.x - parent.width / 2, maskPos.y, parent.width, parent.height);

                let mouse = this.mouse[i];
                let mousePos = parent.convertToWorldSpaceAR(mouse.position);
                let mouseRect = cc.rect(mousePos.x - mouse.width / 2, mousePos.y - mouse.height / 2, mouse.width, mouse.height);

                let mixRect = new cc.Rect();
                maskRect.intersection(mixRect, mouseRect);

                if (mixRect.contains(event.getLocation())) {
                    this.hammer.position = parent.position;
                    this.hammer.active = true;

                    this.count++;
                    this.txtCount.string = this.count;
                    break;
                }

            }
        } ,this);

        this.node.on(cc.Node.EventType.TOUCH_END, function(event){
            this.hammer.active = false;
        }, this);
    },

    logic () {
        let index = Math.floor(Math.random() * 9);
        cc.log(`index:${index}`);
        if (index < 0 || index >= this.mouse.length) {
            return;
        }
        this.mouse[index].getComponent('mouse').appear();
    },

    // update (dt) {},
});

原创文章,作者:李掌柜的博客,如若转载,请注明出处:https://www.zengqueling.com/%e3%80%90%e6%89%93%e5%9c%b0%e9%bc%a0%e3%80%91/

联系我们

15602395067

在线咨询:点击这里给我发消息

邮件:eden7@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code