// Learn TypeScript:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
// @property(cc.Label)
// label: cc.Label = null;
// @property
// text: string = 'hello';
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.node.on('touchstart',this.onTouchStart,this);
this.node.on('touchmove',this.onTouchMove,this);
this.node.on('touchend',this.onTouchCancel,this);
this.node.on('touchcancel',this.onTouchCancel,this);
}
onTouchStart(e : cc.Event.EventTouch){
}
onTouchMove(e : cc.Event.EventTouch){
let parent : cc.Node = this.node.parent;
let **pos** : cc.Vec2 = parent.convertToNodeSpaceAR( e.getLocation());
this.node.setPosition( pos );
let direction : cc.Vec2 = pos.normalize();
let maxR = 100 * 0.8;
let r : number = cc.Vec2.**distance**(pos, cc.v2(0,0))//实际距离
if( r > maxR)
{
pos.x = maxR * direction.x;
pos.y = maxR * direction.y;
}
this.node.setPosition(pos)
}
// let direction : cc.Vec2 = pos.normalize();
// let maxR = 100 * 0.8;
// let r : number = cc.Vec2.distance(pos, cc.v2(0,0));
// if( r > maxR)
// {
// pos.x = maxR * direction.x;
// pos.y = maxR * direction.y;
// }
// // cc.log("相对位置: " + pos.x + ", " + pos.y);
// this.node.setPosition( pos);
onTouchCancel(){
this.node.setPosition( cc.v3(0,0,0));
}
update (dt) {
}
}