import { ryw_Event } from "../../../FrameWork/Event/EventEnum"; import EventMgr from "../../../FrameWork/Event/EventMgr"; //背景移动 const { ccclass, property } = cc._decorator; @ccclass export default class BGMoveScript extends cc.Component { @property(cc.Node) mapNode: cc.Node = null; @property() touchScale: number = 1; maskSize: cc.Size = null; startTime_ = 0; start() { this.openTouchEvent(this.mapNode) EventMgr.onEvent_custom(ryw_Event.openBGMove, this.touchEvent, this); } protected onDisable(): void { EventMgr.offEvent_custom(ryw_Event.openBGMove, this.touchEvent, this); } touchEvent(data_) { if (data_.open) { this.openTouchEvent(this.mapNode) } else { this.closeTouchEvent(this.mapNode) } } openTouchEvent(node) { node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this) node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancelNode, this) node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, 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_CANCEL, this.touchCancelNode, this) node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this) node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this) } touchStartNode(event) { this.startTime_ = new Date().getTime(); this.maskSize = new cc.Size(this.node.getContentSize().width / 2, this.node.getContentSize().height / 2); } touchMoveNode(event) { let target = event.target; let delta = event.getDelta().mul(this.touchScale); if (this.mapNode.x + delta.x < this.maskSize.width && this.mapNode.x + delta.x > - this.maskSize.width) { this.mapNode.x += delta.x; } if (this.mapNode.y + delta.y < this.maskSize.height && this.mapNode.y + delta.y > - this.maskSize.height) { this.mapNode.y += delta.y; } } touchEndNode(event) { } touchCancelNode(event) { } // update (dt) {} }