真行业新人,看了api文档但是不知道怎么弄,觉得要使用getPreviousLocationInView(),但是不知道怎么调到。
代码如下:
import { _decorator, Component, systemEvent, SystemEvent, EventMouse, AnimationComponent, Node, Vec2, Vec3 } from “cc”;
const { ccclass, property } = _decorator;
@ccclass(“playerController”)
export class playerController extends Component {
@property({type:Node})
public touchNode: Node=null;
@property({type:Node})
public rotaTarget: Node=null;
@property({type:false})
private isMoving = false;
@property({type:Vec2})
public startPos: Vec2;
@property
public touchState = 1;
@property({type:Vec2})
public endPos: Vec2;
@property
public moveSpeed = 100;
start() {
}
onLoad() {
this.touchState = 0;
this.startPos = cc.v2(0, 0);
this.endPos = cc.v2(0, 0);
this.isMoving = false;
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.TouchCon, this);
}
TouchCon(event) {
console.log("手指移动了");
//触摸点的世界坐标
var pos = new cc.Vec2(event.getLocationX(), event.getLocationY());
//转换为UI坐标.
console.log("未转换前的UI坐标:" + pos);
pos = this.node.getWorldPosition (pos); //这里不知道怎么写,这个写法是错的。
console.log("转换后的UI坐标:" + pos);
// var temp= cc.Vec3(pos.x,pos.y,touchNode.position.z);
//给要移动的物体赋值
this.touchNode.position = pos;
//角度范围控制
if (this.rotaTarget.eulerAngles.y > 360)
this.rotaTarget.eulerAngles = new Vec3(0, 0, 0);
//要旋转的角度
var tempRota = pos.x / 1000;
//旋转目标物体
this.rotaTarget.eulerAngles = new Vec3(0, tempRota, 0);
//只移动x轴,Y轴同理
//this.weapon.x=pos.x;
console.log("当前物体的位置:" + this.touchNode.position);
console.log("当前物体的角度:" + this.rotaTarget.eulerAngles.toString());
}
}