var evt = require("evt");
cc.Class({
extends: cc.Component,
properties: {
val: -1, // 元素標識
right_q_uid: -1, // 正確的節點
is_have_ref_node: true, // true: 亂拖放,則cur_q_uid清爲-1, false:表示cur_q_uid不清除,需要更新origin_pos
cur_q_uid: -1, // 表示沒有佔有任何坑
origin_pos: cc.v2(0.0), // 原始位置,沒有坑時,用於復位
is_can_move: true, // 是否可以移動,問題解答後,不可移動
is_lock: false,
},
onLoad() {
this.init_ref_pos();
this.add_touch_event();
this.init_event_manager();
},
onDestroy: function () {
evt.getInstance().removeByNode(this);
},
on_start_event: function (event) {
event.stopPropagation();
if (!this.is_can_move) {
return;
}
this.node.zIndex = 100;
var time = new Date().getTime();
this.touch_id = this.node._name + "_" + time;
evt.getInstance().emit("check_touch_id", {});
},
on_move_event: function (event) {
event.stopPropagation();
if (!this.is_can_move) {
return;
}
var w_pos = event.getLocation();
var node_local_pos = this.node.parent.convertToNodeSpaceAR(w_pos);
this.node.position = node_local_pos;
},
on_end_or_cancel_event: function (event) {
event.stopPropagation();
if (!this.is_can_move) {
return;
}
this.node.zIndex = 0;
var w_pos = event.getLocation();
evt.getInstance().emit("match-drag-end", {
val: this.val,
cur_q_uid: this.cur_q_uid,
w_pos: w_pos
});
evt.getInstance().emit("check_add_touch_event", {});
this.touch_id = null;
},
init_ref_pos: function () {
this.origin_pos = this.node.position; // 參考位置
},
reset_pos: function () {
if (this.is_have_ref_node) {
this.cur_q_uid = -1;
}
this.node.position = this.origin_pos;
this.is_can_move = true;
},
add_touch_event: function () {
this.node.on(cc.Node.EventType.TOUCH_START, this.on_start_event, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_move_event, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.on_end_or_cancel_event, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.on_end_or_cancel_event, this);
},
remove_touch_event: function () {
this.node.targetOff(this);
},
init_event_manager: function () {
evt.getInstance().on("match-start-all-right", function (data, then) {
then.is_can_move = false;
}, this);
evt.getInstance().on("check_touch_id", function (data, then) {
if (!then.touch_id) {
then.remove_touch_event();
}
}, this);
evt.getInstance().on("check_add_touch_event", function (data, then) {
if (!then.touch_id) {
then.add_touch_event();
}
}, this);
}
});