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) {},
});