UIAccelerometer 加速计(转)

UIAccelerometer加速计是用来检测iphone手机在x.y.z轴三个轴上的加速度。要获得此类调用:
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
同时,你需要设置它的delegate。
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = 1.0/60.0;
委托方法:- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration是表示加速度类。包含了来自加速计UIAccelerometer的真是数据。它有3个属性的值x、y、z。iphone的加速计支持最高以每秒100次的频率进行轮询。此时是60次。
1) 应用程序可以通过加速计来检测摇动,如:用户可以通过摇动iphone擦除绘图。
也可以用户连续摇动几次iphone,执行一些特殊的代码:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static NSInteger shakeCount = 0;
static NSDate *shakeStart;
NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)
{
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
[now release];
[checkDate release];
if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)
{
shakeCount++;
if (shakeCount > 4)
{
// -- DO Something
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}
}
2) 加速计最常见的是用作游戏控制器。在游戏中使用加速计控制对象的移动! 在简单情况下,可能只需获取一个轴的值,乘上某个数(灵敏度),然后添加到所控制对象的坐标系中。在复杂的游戏中,因为所建立的物理模型更加真实,所以必须 根据加速计返回的值调整所控制对象的速度


在cocos2d中接收加速计输入input.使其平滑运动,一般不会去直接改变对象的position.通过:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// -- controls how quickly velocity decelerates(lower = quicker to change direction)
float deceleration = 0.4; 
// -- determins how sensitive the accelerometer reacts(higher = more sensitive)
float sensitivity = 6.0;
// -- how fast the velocity can be at most
float maxVelocity = 100;
// adjust velocity based on current accelerometer acceleration
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
// -- we must limit the maximum velocity of the player sprite, in both directions
if (playerVelocity.x > maxVelocity)
{
playerVelocity.x = maxVelocity;
}
else if (playerVelocity.x < - maxVelocity)
{
playerVelocity.x = - maxVelocity;
}
}
上面deceleration是减速的比率,sensitivity是灵敏度。maxVelocity是最大速度,如果不限制则一直加大就很难停下来。
playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
中 playervelocity是一个速度向量。是累积的。
- (void) update: (ccTime)delta
{
// -- keep adding up the playerVelocity to the player's position
CGPoint pos = player.position;
pos.x += playerVelocity.x;
// -- The player should also be stopped from going outside the screen
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float imageWidthHalved = [player texture].contentSize.width * 0.5f;
float leftBorderLimit = imageWidthHalved;
float rightBorderLimit = screenSize.width - imageWidthHalved;
// -- preventing the player sprite from moving outside the screen
if (pos.x < leftBorderLimit)
{
pos.x = leftBorderLimit;
playerVelocity = CGPointZero;
}
else if (pos.x > rightBorderLimit)
{
pos.x = rightBorderLimit;
playerVelocity = CGPointZero;
}
// assigning the modified position back
player.position = pos;

}

你可能感兴趣的:(UIAccelerometer 加速计(转))