ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cocos 手柄移动小车

2021-09-17 18:02:54  阅读:208  来源: 互联网

标签:node cocos 手柄 cc 小车 pos js let car


这个是实现手柄移动小车的js部分,具体素材就不上传了


按钮的js文件


cc.Class({
    extends: cc.Component,

    properties: {

        car:null,
        cat_js:null
    },

    // LIFE-CYCLE CALLBACKS:

    onl oad () {
        this.car=cc.find('Canvas/汽车')
        //方法1 获取车的 js 文件
       // this.car_js=this.car.getComponent('car')


        this.node.on('touchstart', this.ontouchstart, this)
        this.node.on('touchmove', this.ontouchmove, this)
        this.node.on('touchend', this.ontouchend, this)
        this.node.on('touchcancel', this.ontouchcancel, this)
    },
    ontouchstart(e){
        //cc.log('开始',e.getLocation())
    },
    ontouchmove(e){
        let fu=this.node.parent
        let newVec2 = fu.convertToNodeSpaceAR(e.getLocation()); //世界坐标,转换本地坐标


        let jiaodu=newVec2.normalize() //得到角度值
        let chang=cc.Vec2.distance(cc.v2(0, 0),newVec2) //两个坐标距离
        let max_r=99 //最大区域
        if(chang > max_r){
            newVec2.x=max_r * jiaodu.x
            newVec2.y=max_r * jiaodu.y
        }
        this.node.setPosition(newVec2)

        //用角度值,转换为,夹角
        let jiajiao=jiaodu.signAngle(cc.v2(1, 0))
        //由夹角转化为 弧度
        let hudu=jiajiao/ Math.PI * 180;
        //cc.log(333,this.car)
        this.car.angle=-hudu

        //设置 移动角度
        //方法1,设置角度
      //  this.car_js.fangxiang=jiaodu

        // 下面是方法2
        let pos= this.car.getPosition()
        pos.x+=jiaodu.x*3
        pos.y+=jiaodu.y*3
        this.car.setPosition(pos)


       // cc.log('移动',e)
    },
    move(){
        
    },
    ontouchend(e){
        this.node.setPosition(cc.v2(0, 0))
        //方法1 车停止运行
       // this.car_js.fangxiang=null
      //  cc.log('结束',e)
    },
    ontouchcancel(e){
        this.node.setPosition(cc.v2(0, 0))
        //方法1 车停止运行
       // this.car_js.fangxiang=null
      //  cc.log('中断',e)
    },

    start () {
        cc.log(7777);
    },

    // update (dt) {},
});

这个是车的js 文件


cc.Class({
    extends: cc.Component,

    properties: {
        fangxiang:null

    },
   // onl oad () {},
  //  start () { },
    update (dt) {

        if(!this.fangxiang){
            return
        }

        let pos = this.node.getPosition()

       pos.x+=this.fangxiang.x*3
        pos.y+=this.fangxiang.y*3

       this.node.setPosition(pos)
        //cc.log(new Date().getTime())
    },
});

标签:node,cocos,手柄,cc,小车,pos,js,let,car
来源: https://blog.csdn.net/weixin_42249565/article/details/120354053

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有