@ccclass('Test')
export class Test extends Component {
//触摸ID
private touchID: number;
private moveForce: Vec2 = new Vec2();
private uiTransform: UITransform;
private touchLocation: Vec2;
private currentPosition: Vec3;
start() {
this.uiTransform = this.node.getComponent(UITransform);
this.node.on(NodeEventType.TOUCH_START, this.touchStart, this);
this.node.on(NodeEventType.TOUCH_MOVE, this.touchMove, this);
this.node.on(NodeEventType.TOUCH_END, this.touchEnd, this);
this.node.on(NodeEventType.TOUCH_CANCEL, this.touchEnd, this);
}
update(deltaTime: number) {
}
private touchStart(e: EventTouch) {
this.touchID = e.getID();
let touchPosition = e.getLocation(this.touchLocation);
let convertPosition =
this.uiTransform.convertToNodeSpaceAR(new Vec3(touchPosition.x, touchPosition.y, 0), this.currentPosition);
this.node.position = convertPosition;
}
private touchMove(e: EventTouch) {
if (this.touchID != e.getID()) {
return;
}
let touchPosition = e.getLocation(this.touchLocation);
console.log("touchPosition" + touchPosition)
let convertPosition =
this.uiTransform.convertToNodeSpaceAR(new Vec3(touchPosition.x, touchPosition.y, 0), this.currentPosition);
console.log("convertPosition" + convertPosition);
this.moveForce = new Vec2(convertPosition.x, convertPosition.y).normalize();
this.node.position = convertPosition;
}
private touchEnd(e: EventTouch) {
if (this.touchID != e.getID()) {
return;
}
this.moveForce = v2();
this.node.position = new Vec3();
}
}
脚本如上,期望实现节点跟随触摸点,但是实现出来节点一直在闪烁。根据log感觉同样的参数计算结果并不一致。这是什么原因呢?有大佬解答吗?