我在我的第一个 Cocos2D 游戏中使用加速度计,它工作正常,我可以使用下面的代码移动精灵但是,当我将方向从 LandscapeLeft 更改为 LandscapeRight 时,精灵停止响应 Y 坐标,并且精灵转到屏幕顶部并且没有正确响应...我相信这是因为更改了设备方向,但我不确定,因为我对应用程序开发很陌生,任何帮助将不胜感激。
这是我正在使用的示例代码...
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
//this determines how sensitive the accelerometer reacts (higher = more sensitive)
NSUserDefaults *defaulObj = [NSUserDefaults standardUserDefaults];
float sensitivity = [defaulObj floatForKey:@"Sensitivity"]; //10.0f
if (!sensitivity) {
sensitivity = 6;
}
// this controls how quickly the velocity decelerates (lower = quicker to change direction)
float deceleration = 0.4f;
static float xVal = 0;
if (!self.isFlag) {
xVal = -acceleration.x;
self.isFlag = TRUE;
}
playerVelocity.x = playerVelocity.x * deceleration + (xVal + acceleration.x) * sensitivity;
playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensitivity;
playerVelocity.y = playerVelocity.y*-1;
}