只需一行代码实现2d节点的触摸移动(鼠标拖动或触摸拖动)组件!
直接上代码:
import { Component, Node, Vec3, _decorator } from "cc";
const { ccclass } = _decorator
/**
* 节点触摸移动组件
*/
@ccclass('TouchMoveCom')
export class TouchMoveCom extends Component {
onLoad() {
/**
* 一行代码实现UI节点的触摸移动
*/
this.node.on(Node.EventType.TOUCH_MOVE, e => this.node.translate(new Vec3(e.getUIDelta().x, e.getUIDelta().y)))
}
}