cocos2dx 3.x重力感应延时BUG解决方案,同样适用于quick-cocos2dx,cocos2dx-js

cocos2dx 3.X版本的重力感应经过一定时间后会非常不灵敏,网上的许多方案是修改帧频,这种解决方案是错误的,官方已经提供了解决方案,如下:

修改cocos2dx目录下的cocos/platform/ios/CCDevice-ios.mm文件

在头部找个合适的地方添加如下常量:

#define SENSOR_DELAY_GAME 0.02

找到

- (id) init

{

    if( (self = [super init]) ) {

        _acceleration = new cocos2d::Acceleration();

        _motionManager = [[CMMotionManager alloc] init];

    }

    return self;

}

修改为:

- (id) init

{

    if( (self = [super init]) ) {

        _acceleration = new cocos2d::Acceleration();

        _motionManager = [[CMMotionManager alloc] init];

        _motionManager.accelerometerUpdateInterval = SENSOR_DELAY_GAME;

    }

    return self;

}

就是在init函数中添加

        _motionManager.accelerometerUpdateInterval = SENSOR_DELAY_GAME;

参考链接:

https://github.com/cocos2d/cocos2d-x/commit/6f2819ec5a3a73efdc834274f9587f92f90949af

文章来源:

Author:recoye
link:http://www.recoye.com/cococ2dx-accelerometer-bug