移动节点,吸附
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
type: 1,
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
},
start() {
//this.direction = 1;
},
touchStartEvent(event) {
//点击旋转
this._zIndex = this.node.zIndex;
this._originPos = this.node.getPosition();
this._startPos = event.getLocation();
this.node.zIndex += 100;
},
touchMoveEvent(event) {
let pos = event.getLocation();
if (!this._startPos) {
return;
}
let offset_x = pos.x - this._startPos.x;
let offset_y = pos.y - this._startPos.y;
this.node.x = this._originPos.x + offset_x;
this.node.y = this._originPos.y + offset_y;
},
touchEndEvent(event) {
this.node.zIndex = 1;
this.node.parent.getComponent("JigsawCtrl").WinCheck();
return;
this._startPos = null;
this.node.zIndex = this._zIndex;
cc.log(cc.find("buttonMap"), this.node.parent.parent)
var Mchildren = cc.find("buttonMap", this.node.parent.parent).children;
if (this.type === 1) {//吸附格子中央
var min = {};
min.length = 99999;
var pos1 = cc.v2(this.node.x, this.node.y);
for (var i = 0; i < Mchildren.length; i++) {
var pos2 = cc.v2(Mchildren[i].x, Mchildren[i].y);
var temp = pos1.sub(pos2);
var dis = Math.abs(temp.mag())
if (min.length > dis) {
min.node = Mchildren[i];
min.length = dis;
}
}
this.node.x = min.node.x;
this.node.y = min.node.y;
}
else if (this.type === 2) {//吸附格子四边中点
var min = {};
min.length = 99999;
var pos1 = cc.v2(this.node.x, this.node.y);
for (var i = 0; i < Mchildren.length; i++) {
var pos2 = cc.v2(Mchildren[i].x + 60, Mchildren[i].y);
var temp = pos1.sub(pos2);
var dis = Math.abs(temp.mag())
if (min.length > dis) {
min.node = Mchildren[i];
min.length = dis;
min.L = true;
}
var pos3 = cc.v2(Mchildren[i].x, Mchildren[i].y + 60);
var temp = pos1.sub(pos3);
var dis = Math.abs(temp.mag())
if (min.length > dis) {
min.node = Mchildren[i];
min.length = dis;
min.L = false;
}
}
if (min.L) {
this.node.x = min.node.x + 60;
this.node.y = min.node.y;
}
else {
this.node.x = min.node.x;
this.node.y = min.node.y + 60;
}
}
/*
this.node.setPosition(GameMgr.GetGridPos(this._row, this._col));
if (Timer.getTimer() > 0) {
GameMgr.StartXiaoChu();
}*/
},
touchCancel() {
this.node.destroy();
},
update(dt) {
},
});
检测拼图是否拼好
// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
point: [cc.Node],
all: cc.Node,
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.winPos = [cc.v2(78.034, -32.726), cc.v2(4.664, 171.186), cc.v2(64.368, 141.513), cc.v2(-75.822, -17.236), cc.v2(9.953, -130.076)];
this.all.active = false;
},
start() {
this.SetStart();
},
//初始化
SetStart() {
this.all.active = false;
for (var i = 0; i < this.point.length; i++) {
this.point[i].x = 10 * i * (Math.floor(Math.random() * 5) + 5) * (Math.floor(Math.random() * 2) - 1);
this.point[i].y = 10 * i * (Math.floor(Math.random() * 5) + 5) * (Math.floor(Math.random() * 2) - 1);
this.point[i].active = true;
}
},
//检测距离,返回两点距离绝对值
GetDestance(p1, p2) {
var angle = Math.atan2((p1.y - p2.y), (p2.x - p1.x));
var theta = angle * (180 / Math.PI);
let distance = p1.sub(p2).mag();
//cc.log(distance)
return distance;
},
//检测是否拼好,在触摸完成后调用
WinCheck() {
var count = 0;
for (var i = 0; i < this.point.length; i++) {
if (this.GetDestance(this.point[i].position, this.winPos[i]) < 20) {
count++;
}
}
if (count === 5) {
cc.log("拼图成功!");
for (var i = 0; i < this.point.length; i++) {
this.point[i].active = false;
}
this.all.active = true;
}
},
// update (dt) {},
});