作者在 2020-02-16 13:59:42 发布以下内容
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) {},
});