微信小游戏:物体拖动

2018-09-19  本文已影响50人  cmd_ts
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property({ type: cc.Node })
    rootNode: cc.Node = null;//

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START,this.touchBegin,this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);        
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
        
    }

    touchBegin(event){
        console.log("touchbegin");
    }

    touchMove(event){
        console.log("touchMove");        
        let per_point = event.getPreviousLocation();//上一次的节点坐标        
        var newx = this.rootNode.getPositionX() + (event.getLocationX() - per_point.x);
        var newy = this.rootNode.getPositionY() + (event.getLocationY() - per_point.y);
        this.rootNode.setPosition(cc.v2(newx,newy));
        
        //this.rootNode.setPosition(cc.v2(this.rootNode));
    }
    
    touchCancel(event){
        console.log("touchCancel");
    }

    start () {

    }

    // update (dt) {}
}

上一篇 下一篇

猜你喜欢

热点阅读