zoukankan      html  css  js  c++  java
  • Cocos Creator 拖动效果

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

    代码DragToAnywhere.ts

    const { ccclass, property } = cc._decorator;
    
    @ccclass
    export 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._onTouchMove, this);
            this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, 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

  • 相关阅读:
    css
    css加号波浪号
    C++对象池
    C++11 智能指针
    C++内存泄漏检测(调试工具)
    JSONP是个嘛玩意?解决跨域问题?
    使用django + KindEditor 开发个人博客系统
    前端文本框插件KindEditor
    jQuery AJAX
    Django ModelForm表单验证
  • 原文地址:https://www.cnblogs.com/rustfisher/p/14233205.html
Copyright © 2011-2022 走看看