// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html 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; // LIFE-CYCLE CALLBACKS: // onLoad () {} 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) } } startTime_ = 0; 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(); } touchCancelNode(event) { this.goToBoundary() } touchMoveNode(event) { let target = event.target let delta = event.getDelta() this.mapNode.x += delta.x //this.mapNode.y += delta.y this.goToBoundary() } touchEndNode(event) { let time_ = new Date().getTime(); this.goToBoundary() // if(time_ - this.startTime_ > 150){ // return // }else{ // } } goToBoundary() { let widthMask = cc.visibleRect.width let heightMask = cc.visibleRect.height let widthMap = this.mapNode.width let heightMap = this.mapNode.height //右边界 if (this.mapNode.x + widthMap / 2 <= widthMask / 2) { this.mapNode.x = widthMask / 2 - widthMap / 2 //左边界 } else if (this.mapNode.x - widthMap / 2 >= -widthMask / 2) { this.mapNode.x = -widthMask / 2 + widthMap / 2 } //上边界 if (this.mapNode.y + heightMap / 2 <= heightMask / 2) { this.mapNode.y = heightMask / 2 - heightMap / 2 //下边界 } else if (this.mapNode.y - heightMap / 2 >= -heightMask / 2) { this.mapNode.y = -heightMask / 2 + heightMap / 2 } } // update (dt) {} }