cocos 触控拖拽移动

编程入门 行业动态 更新时间:2024-10-08 19:45:23

cocos 触控<a href=https://www.elefans.com/category/jswz/34/1761764.html style=拖拽移动"/>

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 触控拖拽移动

本文发布于:2024-02-27 19:42:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拖拽   触控   cocos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!