重力传感事件应用之一 手机摇一摇(摇一次得一分)

手机摇动算法:摇一次只要 x 或y 正负之间变化并超过指定的幅度,就得一分。

        cc.inputManager.setAccelerometerEnabled(true);
        cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);

    onDeviceMotionEvent :function (event) {
        var nowGX = event.acc.x.toFixed(2);
        var nowGY = event.acc.y.toFixed(2);
        var nowGZ = event.acc.z.toFixed(2);


        if(this._isGetScore3(nowGX,nowGY)){
            this.score += 1;
            this.scoreDisplay.string = this.score.toString();
        }


    },


    _isGetScore3(nowGX,nowGY){
        var stand_x = 0.25;
        var stand_y = 0.25;
        if(this.x_prev_tag){  //前一次为 正 
            stand_x = -stand_x;

            if(nowGX <= stand_x){
                var cur_x_tag = false;
            }else{
                var cur_x_tag = true;
            }
        }else {    //前一次为负
            if(nowGX >= stand_x){
                var cur_x_tag = true;
            }else{
                var cur_x_tag = false;
            }
        }

        if(this.y_prev_tag){  //前一次为 正 
            stand_y = -stand_y;

            if(nowGY <= stand_y){
                var cur_y_tag = false;
            }else{
                var cur_y_tag = true;
            }
        }else {    //前一次为负
            if(nowGY >= stand_y){
                var cur_y_tag = true;
            }else{
                var cur_y_tag = false;
            }
        }



        var x_is = false;
        var y_is = false;
        if(( this.x_prev_tag == true && cur_x_tag == false) ||
            (this.x_prev_tag == false && cur_x_tag == true)
        ){

            //console.log('x prev:cur==',this.x_prev_val, '##',nowGX);
            this.x_prev_val = nowGX;
            this.x_prev_tag = cur_x_tag;
            x_is = true;
        }

        if((this.y_prev_tag == true && cur_y_tag == false) ||
            (this.y_prev_tag == false && cur_y_tag == true)
        ){
            //console.log('y prev:cur==', this.y_prev_val, '##',nowGY)
            this.y_prev_val = nowGY;
            this.y_prev_tag = cur_y_tag;
            y_is = true;
        }

        return x_is || y_is;


    },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值