cocos 版本 3.6.2
手指拖拽一个节点,节点跟着手指移动。
import { _decorator, Component, Node, Tween, Vec3, Vec2, Input, log, EventTouch, view } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Test')
export class Test extends Component {
@property(Node)
myTouch: Node
touchStartPoint: Vec2 = new Vec2(0, 0)
start() {
this.myTouch.on(Input.EventType.TOUCH_CANCEL, () => {
log('TOUCH_CANCEL')
this.touchStartPoint = new Vec2(0, 0)
}, this)
this.myTouch.on(Input.EventType.TOUCH_END, () => {
log('TOUCH_END')
this.touchStartPoint = new Vec2(0, 0)
}, this)
this.myTouch.on(Input.EventType.TOUCH_MOVE, (event: EventTouch) => {
let node: Node = event.currentTarget
let pos = new Vec2()
let shit = pos.set(event.getUILocation())
let x = shit.x - view.getVisibleSize().width / 2 - this.touchStartPoint.x
let y = shit.y - view.getVisibleSize().height / 2 - this.touchStartPoint.y
node.setPosition(x, y, 0)
}, this)
this.myTouch.on(Input.EventType.TOUCH_START, (event: EventTouch) => {
let node: Node = event.currentTarget
this.touchStartPoint.set(event.getUILocation())
let x = this.touchStartPoint.x - view.getVisibleSize().width / 2 - node.getPosition().x
let y = this.touchStartPoint.y - view.getVisibleSize().height / 2 - node.getPosition().y
this.touchStartPoint = new Vec2(x, y)
}, this)
}
update(deltaTime: number) {
}
}