cc.Class({
extends: cc.Component,
properties: {
hua: cc.Node,
box_click: cc.Node,
result_layout: cc.Node,
show_result: cc.Node,
maskNode: cc.Node,
},
onLoad() {
this.gou = [];
this.firstClick = true;
this.node.on(cc.Node.EventType.TOUCH_START, this.beginGame, this);
this.box_click.children.forEach(element => {
element.on(cc.Node.EventType.TOUCH_END, this.gouEvent, this);
});
this.hua.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
this.hua.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
this.hua.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
this.hua.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
//鼠标滚轮放大缩小hua的scale
this.hua.on(cc.Node.EventType.MOUSE_WHEEL, this.mouseWheelEvent, this);
this.curGouCount = -1;
this.touchType = '';
Global.gameTime = 90;
this.pointsDis = 0;
this.newPointsDis = 0;
this.startScale = this.hua.scale;
this.maxScale = 1.5;
this.originalTouchDistance = null;
this.originalNodeScale = null;
},
start() {
//背景音乐
globalVar.musicManageScript.playBgMusic();
},
//开始游戏
beginGame(event) {
if (this.firstClick) {
//cc.director.emit('beginGame');
//注销按钮点击事件
this.node.off(cc.Node.EventType.TOUCH_START, this.beginGame, this);
this.firstClick = false;
}
},
//点击到没有勾的地方
touchStartEvent(event) {
let touches = event.getTouches();
if (touches.length == 1) {
console.log('没有gou的位置');
this.touchType = event.type;
}
},
//点击到有勾的地方
gouEvent(event) {
if (this.firstClick) {
//cc.director.emit('beginGame');
//注销按钮点击事件
this.node.off(cc.Node.EventType.TOUCH_START, this.beginGame, this);
this.firstClick = false;
}
this.playClickSound();
if (event.target.getChildByName('gou').active) return;
console.log('有gou的位置');
event.target.getChildByName('gou').active = true;
this.showResultItem(event.target);
},
//检查是否游戏结束
checkSuceess() {
//判断是否所有勾都被点击
let isAllGou = this.box_click.children.every((item, index) => {
return item.getChildByName('gou').active;
})
if (isAllGou) {
if (Global.gameResule == -1) {
Global.gameResule = 1;
cc.director.emit('gameSuccess');
}
}
},
touchMoveEvent(event) {
let touches = event.getTouches();
if (touches.length == 2) {
this.zoom(event, this.hua);
this.restrictPic();
} else if (touches.length == 1) {
// this.touchType = event.type;
// let delta = event.touch.getDelta();
// this.hua.x += delta.x;
// this.hua.y += delta.y;
// this.restrictPic();
// 一根手指是移动
let delta = event.getDelta();
this.hua.setPosition(this.hua.position.add(delta));
this.restrictPic();
}
},
zoom(event, nodeTarget) {
const touches = event.getTouches();
if (touches.length >= 2) {
const touch1 = touches[0].getLocation();
const touch2 = touches[1].getLocation();
// 计算双指间距
const distance = touch1.sub(touch2).mag();
if (!this.originalTouchDistance) {
// 初始化双指初始间距和节点初始缩放
this.originalTouchDistance = distance;
this.originalNodeScale = nodeTarget.scale;
}
// 计算当前缩放比例
const scale = distance / this.originalTouchDistance;
// 计算目标缩放
let targetScale = scale * this.originalNodeScale;
// 确保缩放在minScale和maxScale之间
targetScale = Math.max(this.startScale, targetScale);
targetScale = Math.min(this.maxScale, targetScale);
// 设置节点的新缩放比例
if (scale > 1) {
if (targetScale > this.maxScale) {
targetScale = this.maxScale;
return;
}
} else {
if (targetScale < this.startScale) {
targetScale = this.startScale;
return;
}
}
nodeTarget.scale = targetScale;
}
},
restrictPic() {
// 限制移动,防止出现黑边
let huaWidth = this.hua.getBoundingBox().width;
let huaHeight = this.hua.getBoundingBox().height;
this.maskNode = !this.maskNode ? this.hua.parent : this.maskNode;
if (this.hua.x > 0 && this.hua.x - 0 > huaWidth / 2 - this.maskNode.width / 2)
this.hua.x = huaWidth / 2 - this.maskNode.width / 2;
if (this.hua.x < 0 && this.hua.x - 0 < this.maskNode.width / 2 - huaWidth / 2)
this.hua.x = this.maskNode.width / 2 - huaWidth / 2;
if (this.hua.y > 0 && this.hua.y - 0 > huaHeight / 2 - this.maskNode.height / 2)
this.hua.y = huaHeight / 2 - this.maskNode.height / 2;
if (this.hua.y < 0 && this.hua.y - 0 < this.maskNode.height / 2 - huaHeight / 2)
this.hua.y = this.maskNode.height / 2 - huaHeight / 2;
},
mouseWheelEvent(event) {
let delta = event.getScrollY();
if (delta > 0) {
this.hua.scale += 0.05;
if (this.hua.scale > 1.5) {
this.hua.scale = 1.5;
}
} else if (delta < 0) {
this.hua.scale -= 0.05;
if (this.hua.scale < this.startScale) {
this.hua.scale = this.startScale;
return;
}
}
},
touchEndEvent(event) {
if (this.touchType == 'touchstart') {
cc.director.emit('subTime', event.getLocation());
}
},
//展示结果
showResultItem(target) {
this.curGouCount += 1;
let curGou = target.name;
console.log('name', curGou);
for (let i = 0; i < this.show_result.children.length; i++) {
const element = this.show_result.children[i];
if (element.name == curGou) {
let targetNode = this.result_layout.children[this.curGouCount];
element.position = targetNode.position;
targetNode.getChildByName('icon_name').active = true;
targetNode.getChildByName('icon_name').getComponent(cc.Label).string = this.showName(element.name);
}
}
this.checkSuceess();
},
showName(resuleName) {
let imagerName = '';
switch (resuleName) {
case 'zgrr1_1':
imagerName = '运动兔';
break;
case 'zgrr1_2':
imagerName = '种地兔';
break;
case 'zgrr1_3':
imagerName = '兔崽';
break;
case 'zgrr1_4':
imagerName = '兔妈妈';
break;
case 'zgrr1_5':
imagerName = '兔子玩偶';
break;
case 'zgrr1_6':
imagerName = '兔子气球';
break;
case 'zgrr1_7':
imagerName = '钓鱼兔';
break;
case 'zgrr1_8':
imagerName = '宠物兔';
break;
case 'zgrr1_9':
imagerName = '大白兔糖果';
break;
case 'zgrr1_10':
imagerName = '糖画兔子';
break;
case 'zgrr1_11':
imagerName = '玉兔';
break;
case 'zgrr1_12':
imagerName = '潜水兔';
break;
default:
break;
}
return imagerName;
},
// update (dt) {},
playClickSound() {
//音效
globalVar.musicManageScript.playMusic('Sound/common/点击音效');
},
});
01-30