Cocos2D學習筆記之UIAccelerometer加速計

來源:互聯網
上載者:User

Cocos2D學習筆記之UIAccelerometer加速計是本文要介紹的內容,以UIAccelerometer加速計為執行個體,來看內容。UIAccelerometer加速計是用來檢測iphone手機在x.y.z軸三個軸上的加速度。要獲得此類調用:

 
  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; 

同時,你需要設定它的delegate。

 
  1. UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];  
  2. accelerometer.delegate = self;  
  3. accelerometer.updateInterval = 1.0/60.0; 

委託方法:

 
  1. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration 

是表示加速度類。包含了來自加速計UIAccelerometer的真是資料。它有3個屬性的值x、y、z。iphone的加速計支援最高以每秒100次的頻率進行輪詢。此時是60次。

1、應用程式可以通過加速計來檢測搖動,如:使用者可以通過搖動iphone擦除繪圖。

也可以使用者連續搖動幾次iphone,執行一些特殊的代碼:

 
  1. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration  
  2. {  
  3. static NSInteger shakeCount = 0;  
  4. static NSDate *shakeStart;  
  5. NSDate *now = [[NSDate alloc] init];  
  6. NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];  
  7. if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)  
  8. {  
  9. shakeCount = 0;  
  10. [shakeStart release];  
  11. shakeStart = [[NSDate alloc] init];  
  12. }  
  13. [now release];  
  14. [checkDate release];  
  15. if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)  
  16. {  
  17. shakeCount++;  
  18. if (shakeCount > 4)  
  19. {  
  20. // -- DO Something  
  21. shakeCount = 0;  
  22. [shakeStart release];  
  23. shakeStart = [[NSDate alloc] init];  
  24. }  
  25. }  

2、加速計最常見的是用作遊戲控制器。在遊戲中使用加速計控制對象的移動!在簡單情況下,可能只需擷取一個軸的值,乘上某個數靈敏度),然後添加到所控制對象的座標系中。在複雜的遊戲中,因為所建立的物理模型更加真實,所以必鬚根據加速計返回的值調整所控制對象的速度。

cocos2d中接收加速計輸入input.使其平滑運動,一般不會去直接改變對象的position.通過:

 
  1. - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration  
  2. {  
  3. // -- controls how quickly velocity decelerates(lower = quicker to change direction)  
  4. float deceleration = 0.4;   
  5. // -- determins how sensitive the accelerometer reacts(higher = more sensitive)  
  6. float sensitivity = 6.0;  
  7. // -- how fast the velocity can be at most  
  8. float maxVelocity = 100;  
  9. // adjust velocity based on current accelerometer acceleration  
  10. playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;  
  11. // -- we must limit the maximum velocity of the player sprite, in both directions  
  12. if (playerVelocity.x > maxVelocity)  
  13. {  
  14. playerVelocity.x = maxVelocity;  
  15. }  
  16. else if (playerVelocity.x < - maxVelocity)  
  17. {  
  18. playerVelocity.x = - maxVelocity;  
  19. }  

上面deceleration是減速的比率,sensitivity是靈敏度。maxVelocity是最大速度,如果不限制則一直加大就很難停下來。

 
  1. playerVelocityplayerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity; 

中 playervelocity是一個速度向量。是累積的。

 
  1. - (void) update: (ccTime)delta  
  2. {  
  3. // -- keep adding up the playerVelocity to the player's position  
  4. CGPoint pos = player.position;  
  5. pos.x += playerVelocity.x;  
  6. // -- The player should also be stopped from going outside the screen  
  7. CGSize screenSize = [[CCDirector sharedDirector] winSize];  
  8. float imageWidthHalved = [player texture].contentSize.width * 0.5f;  
  9. float leftBorderLimit = imageWidthHalved;  
  10. float rightBorderLimit = screenSize.width - imageWidthHalved;  
  11. // -- preventing the player sprite from moving outside the screen  
  12. if (pos.x < leftBorderLimit)  
  13. {  
  14. pos.x = leftBorderLimit;  
  15. playerVelocity = CGPointZero;  
  16. }  
  17. else if (pos.x > rightBorderLimit)  
  18. {  
  19. pos.x = rightBorderLimit;  
  20. playerVelocity = CGPointZero;  
  21. }  
  22. // assigning the modified position back  
  23. player.position = pos;  

小結:Cocos2D學習筆記之UIAccelerometer加速計的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.