加速及控制精灵移动,加速计值的处理
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
#define kFilteringFactor 0.1
#define kRestAccelY -0.6
#define kMaxDiffX 0.2
#define kMaxDiffY 0.2
rollingX = (acceleration.x * kFilteringFactor) + rollingX * (1.0 - kFilteringFactor);
rollingY = (acceleration.y * kFilteringFactor) + rollingY * (1.0 - kFilteringFactor);
float accelx = acceleration.x - rollingX;
float accely = acceleration.y - rollingY;
float accelFractionX = accelx / kMaxDiffX;
float pointsPerSecondX = (_winsize.width * 0.5) * accelFractionX;
float accelDiffY = accely - kRestAccelY;
float accelFractionY = accelDiffY / kMaxDiffY;
float pointsPerSecondY = (_winsize.height * 0.5) * accelFractionY;
_planeVelocity.x = pointsPerSecondX;
_planeVelocity.y = pointsPerSecondY;
}
- (void)update:(ccTime)delta {
CGPoint offset = ccpMult(_planeVelocity, delta);
// 加速计控制精灵的移动
[_planeSprite setOffset:offset];
// 飞机转弯背景速度减慢
// 背景滚动速度调整
}