Cocos Creator 拖动效果

Cocos Creator 拖动效果,第1张

概述我们要实现的效果是,按住并拖动一个小物体,物体跟随手指(鼠标)移动。 代码DragToAnywhere.ts const { ccclass, property } = cc._decorator;

我们要实现的效果是,按住并拖动一个小物体,物体跟随手指(鼠标)移动。

代码DragToAnywhere.ts

const { ccclass,property } = cc._decorator;@ccclassexport default class DragToAnywhere extends cc.Component {    @property(cc.Label)    label: cc.Label = null;    start () {            }    onEnable() {        this.node.on(cc.Node.EventType.touch_MOVE,this._ontouchmove,this);        this.node.on(cc.Node.EventType.touch_END,this._ontouchend,this);    }    ondisable() {        this.node.off(cc.Node.EventType.touch_MOVE,this);        this.node.off(cc.Node.EventType.touch_END,this);    }    // update (dt) {}    _ontouchmove(touchEvent) {        let location = touchEvent.getLocation();        this.node.position = this.node.parent.convertToNodeSpaceAR(location); // 确定位置    }    _ontouchend(touchEvent) {        // 放下    }}

把DragToAnywhere.ts挂在预制体上。在场景中创建预制体对象。

let node1 = cc.instantiate(this.drag_item);this.node.addChild(node1);node1.x = 100;node1.y = 100;node1.getComponent(DragToAnywhere).label.string = '水星';

工程请查看github/CCCTry

参考:
Cocos Creator: https://rustfisher.com/categories/CocosCreator/

总结

以上是内存溢出为你收集整理的Cocos Creator 拖动效果全部内容,希望文章能够帮你解决Cocos Creator 拖动效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/998924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存