拖拽移动"/>
cocos 触控拖拽移动
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: NodetouchStartPoint: 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.currentTargetlet pos = new Vec2()let shit = pos.set(event.getUILocation())let x = shit.x - view.getVisibleSize().width / 2 - this.touchStartPoint.xlet y = shit.y - view.getVisibleSize().height / 2 - this.touchStartPoint.ynode.setPosition(x, y, 0)}, this)this.myTouch.on(Input.EventType.TOUCH_START, (event: EventTouch) => {let node: Node = event.currentTargetthis.touchStartPoint.set(event.getUILocation())let x = this.touchStartPoint.x - view.getVisibleSize().width / 2 - node.getPosition().xlet y = this.touchStartPoint.y - view.getVisibleSize().height / 2 - node.getPosition().ythis.touchStartPoint = new Vec2(x, y)}, this)}update(deltaTime: number) {}
}
更多推荐
cocos 触控拖拽移动
发布评论