cocos 手柄移动小车

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


按钮的js文件


cc.Class({
    extends: cc.Component,

    properties: {

        car:null,
        cat_js:null
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        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

    },
   // onLoad () {},
  //  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())
    },
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两个人的幸福online

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值