creator動畫角度

 

cc.Class({
    extends: cc.Component,

    properties: {
        monster:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //使用事件名來註冊
        this.node.on('touchstart',function (event) {
            var pos = event.getLocation();
            pos = this.node.convertToNodeSpaceAR(pos);
            var start = this.monster.getPosition();
            var end = pos;
            var rot = this.getAngle(start,end);
            this.monster.rotation = rot;
        },this)
    },

    getAngle:function(start,end){
        // 兩點的x,y值
        var x = end.x - start.x;
        var y = end.y - start.y;
        // 斜邊長度
        var hypotenuse = Math.sqrt(x *x + y * y);

        var cos = x / hypotenuse;   
        var radian = Math.acos(cos);

        // 求出弧度
        var angle = 180 / (Math.PI /radian);

        // 用弧度算出角度
        if(y < 0){
            angle = 0 - angle;
        }else if(y == 0 && x < 0){
            angle = 180;
        }
        return 90 - angle;
    },

    start () {

    },

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章