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 ItemIconBTouchScript extends cc.Component { @property(cc.Node) touchNode: cc.Node = null; @property(cc.Label) touchName: cc.Label = null; @property(cc.Node) moveNode: cc.Node = null; @property(cc.ScrollView) scrollView: cc.ScrollView = null; // onLoad() {} setTouchNodeIcon(data_) { Common5.getSpriteFrameFromBundle(data_.bandleName, data_.url, this.touchNode.getComponent(cc.Sprite)); this.touchName.string = data_.name; this.onTouchEvent(this.touchNode, data_.index, data_.checkNodeArray); } onTouchEvent(node, index, checkNodeArray) { node.attr({ touchIndex: index, checkNodeArray: checkNodeArray, 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) EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false }); } 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) { if (iterator.active && Common5.checkIntersectsBox(target, iterator)) { checkNode = iterator; break; } } if (checkNode) { Common5.playEffect('放置成功'); this.node.active = false; checkNode.active = false; if (checkNode.targetNode) { checkNode.targetNode.active = true; } target.attr({ touchIndex: target.touchIndex, checkNode: checkNode, }); EventMgr.emitEvent_custom(ryw_Event.itemIconTouchTrue, { targetNode: target }); } else { Common5.playEffect('放置错误'); EventMgr.emitEvent_custom(ryw_Event.itemIconTouchFalse, { targetNode: target }) } if (target["recoveposi"]) { if (target["recoveparent"]) { this.nodeMoveToRecovery(target, target["recoveposi"], target["recoveparent"]); } else { this.nodeMoveToRecovery(target, target["recoveposi"]); } } EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: true }); } 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() } }