// Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; @ccclass export default class NewClass extends cc.Component { private isDragging: boolean = false; private touchOffset: cc.Vec2 = cc.v2(0, 0); onLoad() { // Enable touch on this node directly this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); } onTouchStart(event: cc.Event.EventTouch) { this.isDragging = true; // Get touch location in parent coordinates const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation()); // Calculate offset between jet center and touch point this.touchOffset = this.node.position.sub(cc.v2(touchPos.x, touchPos.y)); } onTouchMove(event: cc.Event.EventTouch) { if (!this.isDragging) return; // Convert touch position to parent space const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation()); // Keep the offset so dragging feels natural (finger stays on the same point) this.node.setPosition(cc.v2(touchPos.x, touchPos.y).add(this.touchOffset)); } onTouchEnd(event: cc.Event.EventTouch) { this.isDragging = false; } onDestroy() { this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); } }