美文网首页
微信小游戏:物体拖动

微信小游戏:物体拖动

作者: cmd_ts | 来源:发表于2018-09-19 10:57 被阅读50次
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) {}
}

相关文章

网友评论

      本文标题:微信小游戏:物体拖动

      本文链接:https://www.haomeiwen.com/subject/pjnbnftx.html