Cocos2D學習筆記中UIAccelerometer加速計案例實現(xiàn)
Cocos2D學習筆記中UIAccelerometer加速計案例實現(xiàn)是本文要介紹的內容,主要是來了解UIAccelerometer加速計的實現(xiàn)。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的真是數(shù)據(jù)。它有3個屬性的值x、y、z。iphone的加速計支持最高以每秒100次的頻率進行輪詢。此時是60次。
(1) 應用程序可以通過加速計來檢測搖動,如:用戶可以通過搖動iphone擦除繪圖。
也可以用戶連續(xù)搖動幾次iphone,執(zhí)行一些特殊的代碼:
- - (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) 加速計最常見的是用作游戲控制器。在游戲中使用加速計控制對象的移動!在簡單情況下,可能只需獲取一個軸的值,乘上某個數(shù)(靈敏度),然后添加到所控制對象的坐標系中。在復雜的游戲中,因為所建立的物理模型更加真實,所以必須根據(jù)加速計返回的值調整所控制對象的速度。
在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
- playerVelocityplayerVelocity.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是最大速度,如果不限制則一直加大就很難停下來。
- playerVelocityplayerVelocity.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;
- }
小結:Cocos2D學習筆記中UIAccelerometer加速計案例實現(xiàn)的內容介紹完了,希望通過本文的學習能對你有所幫助!