
cc.Class({
    extends: cc.Component,

    properties: {
        yaogan:cc.Node,
        max_redir:80,
        mix_r:20,
        dir:cc.v2(0,0)
    },


    onLoad () {
        this.yaogan.setPosition(cc.v2(0,0));
        this.yaogan.on(cc.Node.EventType.TOUCH_MOVE,this.onyaoganMove,this);
        this.yaogan.on(cc.Node.EventType.TOUCH_END,this.onyaoganEnd,this);
        this.yaogan.on(cc.Node.EventType.TOUCH_CANCEL,this.onyaoganCancel,this);
    },
//当手指在目标节点区域内离开屏幕时
    onyaoganEnd(event){
        console.log("停止")
        this.yaogan.setPosition(cc.v2(0,0));
        this.dir=cc.v2(0,0);
    },
//摇杆
    onyaoganMove(event){
        // console.log(event)
        let screen_pos =event.getLocation();
        let pos=this.node.convertToNodeSpaceAR(screen_pos);
        let lan=pos.mag();
        console.log("lan",pos)
        // if (lan<=this.mix_r){
        //     return;
        // }
        this.dir.x=pos.x/lan //cos
        this.dir.y=pos.y/lan //sin
        // console.log("dir",this.dir)
        if (lan>this.max_redir) {
            pos.x=pos.x*this.max_redir/lan;
            pos.y=pos.y*this.max_redir/lan;
        }
        this.yaogan.setPosition(pos)

        // console.log("位置",screen_pos)
    },


//当手指在目标节点区域外离开屏幕时
    onyaoganCancel(){
        this.yaogan.setPosition(cc.v2(0,0));
        this.dir=cc.v2(0,0);
    },
    start () {

    },
    // update (dt) {},
});























































































































































































































































































