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;

}
相關文章
相關標籤/搜索