cocos creator 控制小车移动关键代码

 

 
//根据固定点,将捕捉到的鼠标事件,世界坐标转换成节点坐标
var position = this.node.parent.convertToNodeSpaceAR(e.getLocation())
 
 
// 该点所在的方位 (cos, sin)   获取方位
 let direction:cc.Vec2=position.normalize();
 
//获取托盘半径
 let r : number = cc.Vec2.distance(position,cc.v2(0,0))
 
 //小车旋转角度
 let radian = position.signAngle(cc.v2(1, 0));     //计算点击位置的弧度   弧度=弧长/半径   
 let ang = radian / Math.PI * 180;     //弧度制转换为角度值   弧度=π/180×角度      得出     角度=弧度*180/π 
 this.car.getComponent("car").ang = -ang;    //逆时针为正
 
 //控制小车移动
this.car.getComponent("car").direction = direction
 this.node.setPosition(position)
 
 
//小车:
     update () {
         //没有控制
        if(this.direction==null){
            return;
        }
        //旋转
        this.node.angle = this.ang
        //移动
        this.node.x += this.speed*this.direction.x
        this.node.y += this.speed*this.direction.y
     }
posted @ 2021-12-17 15:55  qcjdp  阅读(409)  评论(0编辑  收藏  举报

Bill

Jerry

Evil