import { ryw_Event } from "../../../FrameWork/Event/EventEnum"; import EventMgr from "../../../FrameWork/Event/EventMgr"; import Common5 from "../../../Platform/th/Common5"; const { ccclass, property } = cc._decorator; @ccclass export default class ZhuangbanBIconNode extends cc.Component { @property(cc.Node) touchNode: cc.Node = null; @property(cc.Node) moveNode: cc.Node = null; @property(cc.ScrollView) scrollView: cc.ScrollView = null; // onLoad() {} setTouchNodeIcon(data_) { Common5.getSpriteFrameFromBundle("WordGame", data_.url, this.touchNode.getComponent(cc.Sprite)); this.onTouchEvent(this.touchNode, data_.id, data_.checkNodeArray, data_.hechengData); } onTouchEvent(node, id, checkNodeArray, hechengData) { node.attr({ iconIndex: id, checkNodeArray: checkNodeArray, hechengData: hechengData, recoveposi: node.getPosition(),//初始位置 recoveparent: node.parent,//初始父节点 }); this.openTouchEvent(node) } cancelTouchEvent() { this.closeTouchEvent(this.touchNode); } openTouchEvent(node) { node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this) node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this) node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this) node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this) } closeTouchEvent(node: cc.Node) { node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this) node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this) node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this) node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this) } touchStartNode(event) { let target = event.target as cc.Node; this.scrollView.enabled = false; target.parent = this.moveNode; let posi = event.getLocation()//世界坐标 posi = target.parent.convertToNodeSpaceAR(posi) target.setPosition(posi) } touchMoveNode(event) { let posi = event.getLocation()//世界坐标 let target = event.target posi = target.parent.convertToNodeSpaceAR(posi) target.setPosition(posi) } touchEndNode(event) { let target = event.target; this.scrollView.enabled = true; let checkNodeArray = target.checkNodeArray as cc.Node[]; let checkNode = null; for (let iterator of checkNodeArray) { console.log("++++++++++++++", iterator.name); if (iterator.active && Common5.checkIntersectsBox(target, iterator)) { checkNode = iterator; break; } } if (checkNode) { Common5.playEffect('放置成功'); if (checkNode.name == "zhuangbanIconNode") { this.node.active = false; let scr = checkNode.getComponent(ZhuangbanBIconNode); scr.cancelTouchEvent(); if (target.hechengData) { scr.setTouchNodeIcon(target.hechengData); } } else { this.node.active = false; checkNode.active = false; if (checkNode.targetNode) { checkNode.targetNode.active = true; } target.attr({ iconIndex: target.iconIndex, checkNode: checkNode, }); EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target }); } } else { Common5.playEffect('放置错误'); } if (target["recoveposi"]) { if (target["recoveparent"]) { this.nodeMoveToRecovery(target, target["recoveposi"], target["recoveparent"]); } else { this.nodeMoveToRecovery(target, target["recoveposi"]); } } } nodeMoveToRecovery(node, oldPosi: cc.Vec2, oldParent?: cc.Node, func?: Function) { if (!oldParent) { oldParent = node.parent; } cc.tween(node) .set({ parent: oldParent }) .to(0.1, { x: oldPosi.x, y: oldPosi.y }) .call(() => { if (func) { func() } }) .start() } }